我试图使用Python从Twitter网页中抓取数据,但我没有得到数据,而是一直得到"Javascript不可用“。我在浏览器(Chrome)中启用了Javascript,但是没有任何变化。
这是错误->
<h1>JavaScript is not available.</h1>
<p>We’ve detected that JavaScript is disabled in this browser. Please enable JavaScript or switch to a supported browser to continue using twitter.com. You can see a list of supported browsers in our Help Center.</p>
这是代码->
from bs4 import BeautifulSoup
import requests
url = "https://twitter.com/search?q=%23developer%20advocate&src=typed_query&f=user"
source_code = requests.get(url).text
soup = BeautifulSoup(source_code, "lxml")
content = soup.find("div")
print(content)
我已经尝试在我的浏览器(Chrome)中启用Javascript,我希望返回所需的数据,而错误"Javascript不可用“仍然存在。
发布于 2022-10-31 14:48:39
我绝不会建议通过违反他们的政策来刮推特,你应该使用API来代替!但是对于Javascript部分,只需在请求的头中传递用户代理即可。
from bs4 import BeautifulSoup
import requests
user_agent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
headers = {'User-Agent': user_agent}
url = "https://twitter.com/search?q=%23developer%20advocate&src=typed_query&f=user"
source_code = requests.get(url, headers=headers).text
soup = BeautifulSoup(source_code, "lxml")
content = soup.find("div")
print(content)
https://stackoverflow.com/questions/74263895
复制相似问题