【问题标题】:Beautiful Soup 4 find method returns 'None'Beautiful Soup 4 查找方法返回“无”
【发布时间】:2021-07-29 12:36:20
【问题描述】:
from bs4 import BeautifulSoup
import requests
import re

username = input('enter username: ')

url = f'https://www.tiktok.com/@{username}'
response = requests.get(url).text
soup = BeautifulSoup(response, 'html.parser')
subname = soup.find('h1', class_="share-sub-title")
print(subname)

不管我搜索什么,它只返回“无”。请帮助我,过去 3 天我一直在寻找这个解决方案,但我找不到它。我也是编码新手,所以我很抱歉。

【问题讨论】:

  • 尝试打印响应并验证您是否确实得到了预期的响应。
  • 事实上你在response 中什么也得不到,只是一个空字符串。尝试使用selenium

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

我不确定这是否是您的代码中的问题,但您可以试试这个。在您的请求对象中添加标头。

headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686)"
response = requests.get(url, headers=headers).text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多