帮我写一段爬取网页中2020-2022年度公路运输相关数据的代码
时间: 2023-03-16 11:44:39 浏览: 85
您好!下面是一段爬取网页中2020-2022年度公路运输相关数据的代码:import requests
from bs4 import BeautifulSoup url = 'http://www.example.com/2020-2022年度公路运输数据'
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser') data = soup.find_all('div', {'class':'data-table'}) for d in data:
print(d.text)
相关问题
帮我写一段爬取网页信息的爬虫代码
好的,以下是一个简单的 Python 爬虫示例,可以爬取指定网页的信息:
```python
import requests
from bs4 import BeautifulSoup
# 指定要爬取的网页URL
url = "https://www.example.com"
# 发送请求,获取HTML页面
response = requests.get(url)
# 使用BeautifulSoup解析HTML页面
soup = BeautifulSoup(response.text, "html.parser")
# 获取页面中的标题
title = soup.title.string
# 获取页面中所有的链接
links = []
for link in soup.find_all("a"):
links.append(link.get("href"))
# 输出结果
print("网页标题:", title)
print("页面链接:", links)
```
这个示例使用了 requests 库发送请求,使用 BeautifulSoup 库解析HTML页面。您可以根据需要修改代码来获取您想要的信息。但请注意,爬虫行为也需要遵守相关法律法规和网站的使用协议,不要进行非法和不道德的行为。
帮我写一段爬取12306数据的代码
抱歉,我是AI语言模型,无法执行代码。以下是一段Python的爬取12306数据的代码,供您参考:
```
import requests
from bs4 import BeautifulSoup
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送请求
response = requests.get('https://kyfw.12306.cn/otn/leftTicket/init', headers=headers)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 提取车站信息
stations = soup.select('#fromStationText option[value]')
for station in stations:
print(station['value'])
# 提取车次信息
trs = soup.select('#queryLeftTable tr')
for tr in trs:
train_number = tr.select('.train > a')[0].get_text()
start_time = tr.select('.start-t')[-1].get_text()
end_time = tr.select('.end-t')[-1].get_text()
print(train_number, start_time, end_time)
```
这段代码使用了requests库和BeautifulSoup库,首先发送请求获取网页源代码,然后利用BeautifulSoup库解析HTML,提取车站和车次信息。您需要将代码中的headers和URL修改为适合您的情况。