Python爬虫----------------免费爬取站长工具第一页长尾关键词
有句话说的好,白嫖它不香吗,废话不多说直接上代码
import urllib.request as request
import lxml.html as html
import time
import requests
from bs4 import BeautifulSoup
from openpyxl import load_workbook, workbook
from lxml import etree
import xlwt
from pyquery import PyQuery as pq
def search_keyword(keyword):
a = 1
i=1
num =[]
word=[]
wb = workbook.Workbook() # 创建Excel对象
ws = wb.active # 获取当前正在操作的表对象
# 往表中写入标题行,以列表形式写入!
k = 0
ws.append(['序号', '长尾词', '指数'])
while a <= 1:
url = 'https://data.chinaz.com/keyword/allindex/%s/%s' % (keyword, a)
print(url)
requests.packages.urllib3.disable_warnings()
data = requests.get(url, verify=False)
soup = BeautifulSoup(data.text, 'lxml') # 将爬取的网页以字符串的形式传入
lilist = soup.find_all('li',class_="w60") # 选中class选择器
linkss = soup.find_all('em', class_="blue2")
for lin in range(1,len(linkss)):
if lin<=len(linkss):
num.append(linkss[lin].text)
lin += 2
for aa in range(1,len(lilist)):
word.append(lilist[aa].text)
a += 1
time.sleep(3)
for k in range(0, len(word)): # 按行插入数据到表中
ws.append([k + 1, word[k],num[k]])
wb.save(f'F:/{keyword}.xlsx')
print(f"保存关键词数据为 {keyword}.xls 成功!")
if __name__ == '__main__':
keyword = input('请输入关键词>>')
print('正在查询,请稍后...')
search_keyword(keyword)