4

selenium爬取新榜公众号信息

 2 years ago
source link: https://www.capallen.top/2019/selenium%E7%88%AC%E5%8F%96%E6%96%B0%E6%A6%9C%E5%85%AC%E4%BC%97%E5%8F%B7%E4%BF%A1%E6%81%AF.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

selenium爬取新榜公众号信息

发表于

2019-04-05 更新于 2020-09-15 分类于 工作

阅读次数: Valine: 0

这两天需要对公众号的信息进行爬取,原以为只是爬取信息,而不需要怕公众号,会比较简单,所以尝试了:

  • littlecodersh的ItChat

    可以获取公众号的名称、功能描述以及地区,但并没有把公众号的ID以及认证公司等信息集成进去,而且必须要本人关注之后才能处理;

  • 搜狗微信

    搜狗微信的信息倒是齐全,完全可以满足要求,但是尝试了request+BeatifulSoup还有Selenium之后发现,这个网址真的太小气了,我加了延时,用了代理,依然各种被封,或者是要求输入验证码;

  • 新榜

    只能获取已经收录的公众号信息,好在收录的数量也很多,用request会出现搜索失败,但是用Selenium还是稳的,加了延时,顺利获取了几千条数据。而且,这里还可以查到各个行业时下最热文章、阅读量等等信息。

用selenium爬取代码如下:

from selenium import webdriver
# 引入Keys类包 发起键盘操作
from selenium.webdriver.common.keys import Keys
import time
import csv

def store_data(name,description,company):
#储存信息
writer.writerow((name,description,company))

def get_id(name):
#传入搜索关键词 并进行搜索
driver.find_element_by_id('txt_account').clear()
time.sleep(3)
driver.find_element_by_id('txt_account').send_keys(name)
driver.find_element_by_id('txt_account').send_keys(Keys.ENTER)
time.sleep(10)

#获取信息并储存
description = driver.find_element_by_class_name('wx-description-info').text
company = driver.find_element_by_class_name('auth-span').text
store_data(name,description,company)
time.sleep(30)

#打开浏览器
driver = webdriver.Firefox()
#打开网址
driver.get('https://www.newrank.cn/')

#爬取
error_name = []
csvfile = open('公众号信息.csv','a+')
writer = csv.writer(csvfile)
for name in id_list:
idx = id_list.index(name)
lenght = len(id_list)

try:
if idx%10 == 0:
print(f'{idx+1}/{lenght} is working...')
get_id(name)
except:
error_name.append(name)

csvfile.close()

知识共享许可协议
本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK