【问题标题】:Obtaining Twiiter Likes获得 Twiiter 赞
【发布时间】:2019-09-06 03:48:16
【问题描述】:

我正在尝试提取每个 twitter 上的点赞信息,但它只返回错误的点赞数或根本没有。我很确定我的代码是正确的。我相信这可能是因为 Twitter 试图阻止人们从他们的网站上抓取信息。有没有办法来解决这个问题?还有一种方法可以查看喜欢特定推文的每个人吗?

import re
import requests
import urllib
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from bs4 import BeautifulSoup
import sys
import unittest, time
import openpyxl
url = ["https://twitter.com/CocaCola?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"]
for x in url:
   d = webdriver.Chrome()
   actions = ActionChains(d)
   d.get(x)
   res = requests.get(x)
   page = urllib.urlopen(x)
   numb = 0;
   SCROLL_PAUSE_TIME = 0.5
# Get scroll height
   last_height = d.execute_script("return document.body.scrollHeight")
   while True:
    # Scroll down to bottom
      d.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    # Wait to load page
      time.sleep(SCROLL_PAUSE_TIME)
    # Calculate new scroll height and compare with last scroll height
      new_height = d.execute_script("return document.body.scrollHeight")
      soup = BeautifulSoup(page, "html.parser")
      for posts in soup.findAll('div',{"class":"content"}):
         if(posts.find('p').text.encode('utf-8').find("Retweeted") == -1):
            print(posts.find('span',{"class": "_timestamp js-short-timestamp"}).text)
            print(posts.find('p').text.encode('utf-8'))
            retweet = posts.find('button',{"class": "ProfileTweet-actionButton js-actionButton js-actionFavorite"})
            #print(retweet.find('span',{"class":"ProfileTweet-actionCount"})["data-tweet-stat-count"])
            print(retweet)
            likes = posts.find('div',{"class":"ProfileTweet-action ProfileTweet-action--favorite js-toggleState"})
            print(likes.find('span',{"class": "ProfileTweet-actionCountForPresentation"}))
            numb = numb+1
            if new_height == last_height:
               break
            if numb > 1:
               break
      if numb > 1:
         break
      last_height = new_height
   d.close()

【问题讨论】:

  • 现在这是技术的过度组合!您正在使用 selenium、urllib、requests 和 BeautifulSoup - 最终目标最容易通过 selenium 单独完成(而且,几乎只有通过它,您需要浏览器中的 js 解析器)。您当前的代码中有一个明显的错误 - 您将 urllib 请求所见的源传递给 bs,而不是 selenium。
  • 我听说用beautifulsoup最容易刮掉文字,所以我用了那个

标签: python selenium web beautifulsoup screen-scraping


【解决方案1】:

在这一行:

soup = BeautifulSoup(page, "html.parser")

,您将urllib 请求中看到的源传递给BeautifulSoup,而不是硒。由于urllib 不解析也不执行在 html 中定义/相关的任何 javascript 和 css,因此其内容与浏览器/用户使用的内容不同。

把它改成这个,这样 bs 就可以和渲染的 html 一起工作了:

soup = BeautifulSoup(d.page_source, "html.parser")

【讨论】:

    猜你喜欢
    • 2020-08-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多