Web scraping 用嵌入的tweet抓取网页
我正试图抓取一个网页,其中嵌入了tweetWeb scraping 用嵌入的tweet抓取网页,web-scraping,beautifulsoup,Web Scraping,Beautifulsoup,我正试图抓取一个网页,其中嵌入了tweethttps://thehill.com/homenews/news/376608-west-virginia-teachers-to-continue-strike-after-state-senate-passes-lower-raise。当我从浏览器中使用inspect元素时,它会向嵌入的tweet显示相应的HTML元素,但当我通过页面资源或使用beautifullSoup.findAll()搜索它时,它们不会返回任何结果。如何解决此问题?它是动态的
https://thehill.com/homenews/news/376608-west-virginia-teachers-to-continue-strike-after-state-senate-passes-lower-raise
。当我从浏览器中使用inspect元素时,它会向嵌入的tweet显示相应的HTML元素,但当我通过页面资源或使用beautifullSoup.findAll()搜索它时,它们不会返回任何结果。如何解决此问题?它是动态的,这意味着您需要使用类似于Selenium的工具来呈现页面,然后再拖动页面。但是,该链接位于原始html源中,并带有部分推文,因此您可以继续:
import requests
from bs4 import BeautifulSoup
url = 'https://thehill.com/homenews/news/376608-west-virginia-teachers-to-continue-strike-after-state-senate-passes-lower-raise'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
tweets = soup.find_all('blockquote',{'class':'twitter-tweet'})
for tweet in tweets:
tweet_link = tweet.find('a')['href']
print (tweet_link)