python获取百度热榜链接的实例方法
作者:FXL 时间:2022-10-02 23:10:01
目标网址:
https://www.baidu.com/
要获取的内容:
链接分析:
从下图可以看出只需要获取关键字,再构建就可以了。
完整代码:
import requests
import pprint
import re
import urllib.parse
url = 'https://www.baidu.com/'
headers = {
'Host': 'www.baidu.com',
'Referer': 'https://www.baidu.com/',
'User-Agent': 你的User-Agent,
'Cookie': 你的Cookie
}
response = requests.get(url, headers=headers).content.decode('utf-8')
# 获取关键字
pat = '"pure_title": "(.*?)"'
keyword = re.findall(pat, response, re.S)
print(len(keyword))
for hot_word in keyword:
# 汉字不符合url标准,所以这里需要进行url编码
i = urllib.parse.quote(hot_word, encoding='utf-8', errors='replace')
# url构建
link = f'https://www.baidu.com/s?cl=3&tn=baidutop10&fr=top1000&wd={i}&rsv_idx=2&rsv_dl=fyb_n_homepage&hisfilter=1'
print(link)
你会发现结果很长:
但其实关键字后面的几个参数是可以去掉的,这样url就没有那么长了。
内容扩展:
python 爬取简单的百度搜索结果
爬取百度搜索结果
主要还要借助xpath helper谷歌浏览器的插件来操作更容易找到需要查找信息的xpath位置
还要首先了解一下百度搜索请求的参数 lm默认为0,天数限制,但是好像只有1有用。
默认每页10条信息,rn
pn是页码
from lxml import etree
import re
import requests
import string
import json
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}
response = requests.get('https://www.baidu.com/s?wd=腾讯视频优惠&lm=1',headers=headers)
r = response.text
html = etree.HTML(r,etree.HTMLParser())
r1 = html.xpath('//h3')
r2 = html.xpath('//*[@class="c-abstract"]')
r3 = html.xpath('//a[@class="c-showurl"]')
for i in range(10) :
r11 = r1[i].xpath('string(.)')
r22 = r2[i].xpath('string(.)')
r33 = r3[i].xpath('string(.)')
# with open('test.txt', 'a', encoding='utf-8') as f:
# f.write(json.dumps(r11,ensure_ascii=False) + '\n')
# f.write(json.dumps(r22, ensure_ascii=False) + '\n')
# f.write(json.dumps(r33, ensure_ascii=False) + '\n')
print(r11,end='\n')
print(r22,end='\n')
print(r33)
print()
来源:https://www.py.cn/toutiao/19946.html
标签:python,百度热榜
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python pandas获取csv指定行 列的操作方法
2023-07-13 05:26:46
让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享
2024-05-03 15:50:32
MySQL数据库学习之排序与单行处理函数详解
2024-01-22 13:01:01
numpy中的log和ln函数解读
2023-06-14 22:46:40
![](https://img.aspxhome.com/file/2023/8/60708_0s.jpg)
Pycharm+Scrapy安装并且初始化项目的方法
2023-03-05 11:49:00
![](https://img.aspxhome.com/file/2023/4/66784_0s.jpg)
Golang优雅关闭channel的方法示例
2024-05-03 15:05:44
django实现更改数据库某个字段以及字段段内数据
2024-01-26 14:52:09
![](https://img.aspxhome.com/file/2023/2/89432_0s.jpg)
Python应用之利用pyecharts画中国地图
2023-05-27 16:45:40
![](https://img.aspxhome.com/file/2023/8/90818_0s.png)
1行Go代码实现反向代理的示例
2024-04-28 09:15:26
![](https://img.aspxhome.com/file/2023/6/134016_0s.png)
解析php session_set_save_handler 函数的用法(mysql)
2023-11-17 21:58:28
解决MybatisPlus SqlServer OFFSET 分页问题
2024-01-12 16:26:24
python logging通过json文件配置的步骤
2022-06-04 22:30:19
python统计中文字符数量的两种方法
2022-11-16 00:24:08
Python基础之数据类型详解
2023-11-06 11:01:31
Python argparse中的action=store_true用法小结
2023-07-31 22:35:02
python中mechanize库的简单使用示例
2021-05-22 14:32:12
Python-ElasticSearch搜索查询的讲解
2023-12-30 01:09:27
Mysql合并结果接横向拼接字段的实现步骤
2024-01-19 13:39:35
![](https://img.aspxhome.com/file/2023/3/125673_0s.png)
Python之ascii转中文的实现
2023-08-24 19:39:12
![](https://img.aspxhome.com/file/2023/0/62600_0s.png)
SQL Server作业报错特殊案例分析
2024-01-15 17:33:02
![](https://img.aspxhome.com/file/2023/1/106201_0s.png)