Python实现微信搜一搜批量采集关键词

优采云 发布时间: 2023-05-08 07:24

  微信作为国内使用人数最多的社交软件之一,其搜一搜功能也越来越成为大家获取信息的重要途径。但是,如果需要采集大量的关键词,单个手动复制粘贴显然不太现实。那么,有没有什么方法可以轻松批量采集微信搜一搜中的关键词呢?答案是肯定的。本文将为大家介绍如何利用Python代码实现微信搜一搜批量采集关键词。

  1.模拟登录微信网页版

  在进行微信搜一搜操作之前,我们需要先模拟登录微信网页版,以便后续操作能够进行。具体步骤如下:

   python

#导入所需库

import requests

import re

#构造请求头部信息

headers ={

'User-Agent':'Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}

#获取登录所需的uuid等参数

r = requests.get('https://login.weixin.qq.com/', headers=headers)

uuid = re.findall(r'window.574edab920052f604ce2d34dbd3dc092.code = 200; window.574edab920052f604ce2d34dbd3dc092.uuid ="(.*)";',r.text)[0]

#生成登录二维码

r = requests.get('https://login.weixin.qq.com/qrcode/'+ uuid, headers=headers)

with open('qrcode.jpg','wb') as f:

f.write(r.content)

#扫描二维码登录

while True:

r = requests.get('https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid='+ uuid +'&tip=0&r=c13e039789dcb4bfd86c57b5200c7226&_=1493895409214', headers=headers)

if 'window.code=200' in r.text:e1ab6b5378ebebd758f76592d3e8bb09_uri = re.findall(r'window.redirect_uri="(.*?)"',r.text)[0]+'&fun=new'

s = requests.session()

r =s.get(redirect_uri, headers=headers, allow_redirects=False)

url2 =r.headers['Location']

r =s.get(url2, headers=headers)

break

  2.利用Selenium模拟操作微信网页版

  有了登录凭证之后,我们需要使用Selenium模拟操作微信网页版,以便能够进行微信搜一搜的操作。具体步骤如下:

  

   python

#导入所需库

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.keys import Keys

#启动浏览器并打开微信网页版

options = webdriver.ChromeOptions()

options.add_argument('--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data')

browser = webdriver.Chrome(chrome_options=options)

browser.get('https://wx.qq.com/')

#定位到搜索框并输入关键词,然后点击搜索按钮

search_box = browser.find_element_by_xpath('//*[@id="J_SearchInput"]')

search_box.send_keys('优采云')

search_btn = browser.find_element_by_xpath('//*[@id="J_SearchBtn"]')

ActionChains(browser).move_to_element(search_btn).click().perform()

#模拟下拉操作,以便获取更多的关键词

while True:

browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')

try:

more_btn = browser.find_element_by_xpath('//*[@id="m-addec426932e71323700afa1911f8f1c"]/a')

ActionChains(browser).move_to_element(addec426932e71323700afa1911f8f1c_btn).click().perform()

except:

break

#获取所有搜索结果中的关键词

keywords =[]

items = browser.find_elements_by_xpath('//li[@class="contact_item"]/div[@class="info"]')

for item in items:

keyword = item.find_element_by_xpath('./span[@class="nickname"]').text

if keyword not in keywords:

keywords.append(keyword)

  3.将采集的关键词保存到本地文件

  采集到的关键词可以直接输出到控制台,但是如果需要保存到本地文件中,则需要进行一些额外的操作。具体步骤如下:

   python

#将关键词保存到本地文件

with open('keywords.txt','w', encoding='utf-8') as f:

for keyword in keywords:

f.write(keyword +'\n')

  通过以上三个步骤,我们就可以轻松实现微信搜一搜批量采集关键词的操作了。当然,如果需要采集更多的关键词,也可以通过调整代码中的一些参数进行优化。希望本文对大家有所帮助。

  本文由UWriter编写,如需转载请注明出处:优采云,SEO优化,www.ucaiyun.

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线