使用 python 和 selenium 编写的 twitter 爬虫面临问题

SIM卡

我已经用 python 编写了一个脚本来解析我的 Twitter 个人资料页面中查看所有部分中可用的名称、推文、关注者和关注者。它目前正在做它的工作。但是,我发现这个刮板有两个问题:

  1. 它解析文档的每个页面都卡在任务栏上。
  2. 刮板看起来很笨拙。

这是我写的:

from selenium import webdriver
import time

def twitter_data():

    driver = webdriver.Chrome()
    driver.get('https://twitter.com/?lang=en')

    driver.find_element_by_xpath('//input[@id="signin-email"]').send_keys('username')
    driver.find_element_by_xpath('//input[@id="signin-password"]').send_keys('password')
    driver.find_element_by_xpath('//button[@type="submit"]').click()
    driver.implicitly_wait(15)

    #Clicking the viewall link
    driver.find_element_by_xpath("//small[@class='view-all']//a[contains(@class,'js-view-all-link')]").click()
    time.sleep(10)

    for links in driver.find_elements_by_xpath("//div[@class='stream-item-header']//a[contains(@class,'js-user-profile-link')]"):
        processing_files(links.get_attribute("href"))
        #going on to the each profile falling under viewall section
def processing_files(item_link):

    driver = webdriver.Chrome()
    driver.get(item_link)
    # getting information of each profile holder
    for prof in driver.find_elements_by_xpath("//div[@class='route-profile']"):
        name = prof.find_elements_by_xpath(".//h1[@class='ProfileHeaderCard-name']//a[contains(@class,'ProfileHeaderCard-nameLink')]")[0]
        tweet = prof.find_elements_by_xpath(".//span[@class='ProfileNav-value']")[0]
        following = prof.find_elements_by_xpath(".//span[@class='ProfileNav-value']")[1]
        follower = prof.find_elements_by_xpath(".//span[@class='ProfileNav-value']")[2]
        print(name.text, tweet.text, following.text, follower.text)

twitter_data()

当我发现有必要让机器人等待更长时间时,我在我的刮刀原因中同时使用了implicitly_wait和time.sleep,我使用了后者。提前感谢您查看它。

穆蒂

您可以使用 driver.quit() 关闭页面,如下所示。这将减少任务栏中的页面。

from selenium import webdriver
import time

def twitter_data():

    driver = webdriver.Chrome()
    driver.get('https://twitter.com/?lang=en')

    driver.find_element_by_xpath('//input[@id="signin-email"]').send_keys('username')
    driver.find_element_by_xpath('//input[@id="signin-password"]').send_keys('password')
    driver.find_element_by_xpath('//button[@type="submit"]').click()
    driver.implicitly_wait(15)

    #Clicking the viewall link
    driver.find_element_by_xpath("//small[@class='view-all']//a[contains(@class,'js-view-all-link')]").click()
    time.sleep(10)

    for links in driver.find_elements_by_xpath("//div[@class='stream-item-header']//a[contains(@class,'js-user-profile-link')]"):
        processing_files(links.get_attribute("href"))

    driver.quit()
        #going on to the each profile falling under viewall section
def processing_files(item_link):

    driver1 = webdriver.Chrome()
    driver1.get(item_link)
    # getting information of each profile holder
    for prof in driver1.find_elements_by_xpath("//div[@class='route-profile']"):
        name = prof.find_elements_by_xpath(".//h1[@class='ProfileHeaderCard-name']//a[contains(@class,'ProfileHeaderCard-nameLink')]")[0]
        tweet = prof.find_elements_by_xpath(".//span[@class='ProfileNav-value']")[0]
        following = prof.find_elements_by_xpath(".//span[@class='ProfileNav-value']")[1]
        follower = prof.find_elements_by_xpath(".//span[@class='ProfileNav-value']")[2]
        print(name.text, tweet.text, following.text, follower.text)
        driver1.quit ()

twitter_data()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用python-twitter的问题

来自分类Dev

使用selenium点击链接的网络爬虫问题

来自分类Dev

twitter bot 和 python 的 Tweepy 问题

来自分类Dev

Selenium和Python使用If语句

来自分类Dev

使用python3和selenium进行迭代和检索信息的问题

来自分类Dev

Twitter和Meteor的问题

来自分类Dev

使用Selenium和python捕获AJAX响应

来自分类Dev

使用Python和Selenium Webdriver抓取JavaScript

来自分类Dev

使用Selenium和python捕获AJAX响应

来自分类Dev

使用Selenium和Python获取子元素

来自分类Dev

使用Selenium和Python搜索Google

来自分类Dev

在Selenium和Python中使用Faker

来自分类Dev

如何使用Selenium和Python识别元素

来自分类Dev

使用Python和Selenium验证网址

来自分类Dev

如何使用Selenium和Python上传文件

来自分类Dev

如何使用Selenium和Python抓取注释?

来自分类Dev

使用C / API和C ++类编写Python模块

来自分类Dev

使用Python,GIR和GTK3编写指标

来自分类Dev

使用Python编写和修改VTK多数据文件

来自分类Dev

需要使用Python编写和阅读的帮助

来自分类Dev

使用C / API和C ++类编写Python模块

来自分类Dev

Selenium 和 Python 中的 XPATH 问题

来自分类Dev

使用 AJAX 和 PHP 删除用户记录时面临的问题

来自分类Dev

使用Python获取实际的Facebook和Twitter图像URL

来自分类Dev

使用Python在Twitter上擦除嵌套的Divs和Span?

来自分类Dev

如何使用Selenium和以C#编写的代码执行Cntrl + A功能

来自分类Dev

如何使用Selenium,PHPUnit和Mac OS X编写Internet Explorer的验收测试?

来自分类Dev

使用python和selenium连接到phantomJs Webdriver时出现问题

来自分类Dev

使用Selenium和Python进行网络抓取时出现问题

Related 相关文章

热门标签

归档