利用python爬虫爬取旅游网信息

一、准备需要的库

import requests
from lxml import html
from openpyxl import Workbook

二、爬取的网站

url = 'https://place.qyer.com/china/citylist-0-0-1/'

三、对网站进行抓包分析

利用python爬虫爬取旅游网信息_第1张图片

四、源码

# -- coding: utf-8 --
import requests
from lxml import html
from openpyxl import Workbook

# 创建Excel
wb = Workbook()
ws = wb.active

# 获取数据
url = 'https://place.qyer.com/china/citylist-0-0-1/'

def getpage(url):
    # 请求头,模拟浏览器登录
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'}

    # 访问链接,获取HTML
    r = requests.get(url, headers=headers)
    retext = r.text

    # 解析数据
    ht = html.fromstring(retext)

    # 使用xpath获取
    city = ht.xpath('/html/body/div[5]/div/div[1]/ul/li')
    for i in city:
        name = i.xpath('./h3/a/text()')[0]
        beento = i.xpath('./p[@class="beento"]/text()')[0]
        list = i.xpath('./p[@class="pois"]/a/text()')
        list2 = ''
        # for j in list:
        #     list2=list2+','+j.strip()
        # print(name,beento,list2[1:])
        list = [place.strip() for place in list]
        list2 = ','.join(list)
        datalist = [name, beento, list2]
        ws.append(datalist)

for i in range(1, 10):
    url = 'https://place.qyer.com/china/citylist-0-0-{}/'.format(i)
    getpage(url)

# Excel保存
fileanme = "D:\software\pycharm\pythonProject\04-大三下\课后作业\第3部分爬虫"  # 路径可以自己设置,我这里是python源文件同级目录
wb.save("旅游景点.xlsx")

你可能感兴趣的:(爬虫)