
Python爬取某旅游网站中的中国城市信息
发布日期:2021-05-10 03:34:01
浏览次数:26
分类:原创文章
本文共 2155 字,大约阅读时间需要 7 分钟。
分析
这是目标
可以发现它是通过点击下一页来翻页的,所以可以大概率判断它每一页的链接是有规律的,我们找出它的前两页的链接:
https://place.qyer.com/china/citylist-0-0-1/https://place.qyer.com/china/citylist-0-0-2/
可以发现的确是有规律的,再找一个稍微后一点的页面看看:
https://place.qyer.com/china/citylist-0-0-169/
这下确定无疑了,可以看到,它有171个页面,链接中的数字也是从1开始一直到171,所以可以用一个for循环来提取每一页的内容。
接下来就是分析如何提取一个页中的内容了,我个人最拿手的是xpath,有些人使用的是BeautifulSoup也行。
可以在Chrome的开发者工具中明显看到每一个城市对应一个li标签,所以我先将所有的li标签提取出来,提取结果是一个列表,列表中的每一个对象也是Selector对象,也就是说列表中的每一个li标签还可以使用xpath方法提取该节点中的内容。
接下来就是写好要提取的内容对应的xpath语句了,可以使用Full copy Xpath
或在xpath helper插件中自己写。
代码编写
下面是程序的完整代码:
import requests # the library to initiate a requestfrom fake_useragent import UserAgent # the library to make the request headerimport parsel # the library to parse HTMLimport csv # the library to writer csv filedef getdata(url): headers = { "user-Agent": UserAgent().chrome } response = requests.get(url=url, headers=headers) response.encoding = response.apparent_encoding selector = parsel.Selector(response.text) # extract all li tags lis = selector.xpath('//ul[@class="plcCitylist"]/li') for li in lis: city_names = li.xpath('./h3/a/text()').get() city_names = city_names.rstrip() number_people = li.xpath('./p[2]/text()').get() place_hot = li.xpath('./p[@class="pois"]/a/text()').getall() place_hot = [place.strip() for place in place_hot] place_hot = '、'.join(place_hot) place_url = li.xpath('./p[@class="pics"]/a/@href').get() img_url = li.xpath('./p[@class="pics"]/a/img/@src').get() print(city_names, number_people, place_url, img_url, place_hot, sep='|') with open('qiongyouDate.csv', mode='a', encoding='utf-8', newline='') as file_object: csv_write = csv.writer(file_object) csv_write.writerow([city_names, number_people, place_url, img_url, place_hot])def main(): for i in range(1, 172): url = "https://place.qyer.com/china/citylist-0-0-{}/".format(str(i)) getdata(url)if __name__ == '__main__': main()
运行结果
运行上面的代码,爬取到的数据会打印在控制台中,并且运行完成后会在程序目录中生成一个名为qingyouDate.csv的csv文件,可以使用WPS或Excel将这个文件打开。
下面是运行截图:
下面是生成的csv文件内容截屏:
爬取速度有一点慢。。。还请大家耐心等待
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月08日 13时49分24秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
网页前端查询页面时间显示1605783213000
2021-05-10
Unknown database ‘modd‘
2021-05-10
重载和重写的区别:
2021-05-10
finally,final,finalize() 的区别
2021-05-10
#{}和${}的区别是什么
2021-05-10
nginx服务器访问404
2021-05-10
搭建Vue项目步骤
2021-05-10
docker镜像命令
2021-05-10
docker容器命令
2021-05-10
idea右边maven窗口
2021-05-10
CentOS7使用keepalive实现nginx的高可用
2021-05-10
Centos7.4下mysql5.6开启ssl
2021-05-10
srvctl命令详解
2021-05-10
linux centos6.4 磁盘分区、格式化、挂载
2021-05-10
mysql-5.7 innodb_buffer_pool刷新机制详解
2021-05-10
oracle查看字符集后修改oracle服务端和客户端字符集的步骤
2021-05-10
Oracle闪回查询总结
2021-05-10