用python爬取关键词并解释

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用python爬取关键词并解释相关的知识,希望对你有一定的参考价值。

参考技术A Copyright © 1999-2020, CSDN.NET, All Rights Reserved

python
打开APP

小羊努力搞代码
关注
学习日志:Python 实现网络爬虫——提取关键字 原创
2022-06-19 13:02:38

小羊努力搞代码

码龄174天

关注
编写一段Python代码,向百度提交查询关键词“桃花源记”,抓取百度的查询结果,要求有文字、链接,可以在浏览器中打开抓取的链接,或者调用浏览器打开抓取的链接。

红框内是根据网站信息需要更改的内容。57031baa3a394395be479ad89f1ff15e.png

附上完整代码:

import json
import requests
from lxml import etree
headers =
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) "
"Chrome/88.0.4324.104 Safari/537.36"

response = requests.get('https://www.baidu.com/s?wd=桃花源记&lm=0', headers=headers)
r = response.text
html = etree.HTML(r, etree.HTMLParser())
r1 = html.xpath('//h3')
r2 = html.xpath('//*[@class="content-right_8Zs40"]')
r3 = html.xpath('//*[@class="c-row source_1Vdff OP_LOG_LINK c-gap-top-xsmall"]/a/@href')
for i in range(4):
r11 = r1[i].xpath('string(.)')
r22 = r2[i].xpath('string(.)')
r33 = r3[i]
with open('桃花源记.txt', 'a', encoding='utf-8') as c:
c.write(json.dumps(r11,ensure_ascii=False) + '\n')
c.write(json.dumps(r22, ensure_ascii=False) + '\n')
c.write(json.dumps(r33, ensure_ascii=False) + '\n')
print(r11, end='\n')
print('------------------------')
print(r22, end='\n')
print(r33)

以上是关于用python爬取关键词并解释的主要内容,如果未能解决你的问题,请参考以下文章

python网络编程新浪爬虫:关键词搜索爬取微博数据

Python:输入关键字进行百度搜索并爬取搜索结果

爬虫日记之关键字搜索引擎爬取

python爬虫之selenium+BeautifulSoup库,爬取搜索内容并保存excel

python爬虫之selenium+BeautifulSoup库,爬取搜索内容并保存excel

利用百度搜索结果爬取邮箱