前言:
本篇文章主要作为一个爬虫项目的小练习,来给大家进行一下爬虫的大致分析过程以及来帮助大家在以后的爬虫编写中有一个更加清晰的认识。
一:环境配置
Python版本:3.7
IDE:PyCharm
所需库:requests,bs4,xlwt
二:网页分析
1我们需要去找到user-Agent
三:编写代码
1:导入所需库
import requests
from bs4 import BeautifulSoup
import xlwt
2:编写请求头与参数
url = 'https://trains.ctrip.com/TrainBooking/Search.aspx'
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
'Cookie':'Union=OUID=index&AllianceID=4897&SID=155952&SourceID=&createtime=1693561627&Expires&#