Using Python to Scrape Nested Divs and Spans in Twitter?

后端 未结 1 1292
攒了一身酷
攒了一身酷 2020-12-17 04:10

I\'m trying to scrape the likes and retweets from the results of a Twitter search.

After running the Python below, I get an empty list, []. I\'m not

相关标签:
1条回答
  • 2020-12-17 04:30

    It seems that your GET request returns valid HTML but with no tweet elements in the #timeline element. However, adding a user agent to the request headers seems to remedy this.

    from bs4 import BeautifulSoup
    import requests
    
    url = 'https://twitter.com/search?q=%23bangkokbombing%20since%3A2015-08-10%20until%3A2015-09-30&src=typd&lang=en'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
    r = requests.get(url, headers=headers)
    data = r.text
    soup = BeautifulSoup(data, "lxml")
    all_likes = soup.find_all('span', class_='ProfileTweet-actionCountForPresentation')
    print(all_likes)
    
    0 讨论(0)
提交回复
热议问题