写一个requests爬取https://www.fjmotor.com.cn/allnews_list/tpid_10.html该网站的代码
时间: 2023-11-30 22:05:24 浏览: 27
好的,以下是使用Python的requests库爬取 https://www.fjmotor.com.cn/allnews_list/tpid_10.html 网站的代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.fjmotor.com.cn/allnews_list/tpid_10.html"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 示例:获取所有新闻的标题和链接
news_list = soup.find_all("div", class_="newslist-item")
for news in news_list:
title = news.find("a").text.strip()
link = news.find("a")["href"]
print(title, link)
```
上述代码首先使用requests库发送get请求,获取网页的HTML代码,然后使用BeautifulSoup库解析HTML代码。接着,我们可以通过BeautifulSoup提供的find和find_all方法查找网页中的元素,进而获取我们需要的数据。
以上代码仅供参考,实际应用时需要根据实际情况进行修改。
相关问题
使用requests库爬取https://www.xynu.edu.cn/
好的,以下是使用requests库爬取https://www.xynu.edu.cn/的示例代码:
```python
import requests
url = 'https://www.xynu.edu.cn/'
response = requests.get(url)
if response.status_code == 200:
print(response.text)
else:
print('请求失败')
```
这段代码会发送一个GET请求到https://www.xynu.edu.cn/,并将响应内容打印出来。如果请求成功,响应状态码会是200,否则就会输出"请求失败"。你可以根据自己的需要对这段代码进行修改和扩展。
帮我写一个爬取https://www.luogu.com.cn/contest/68651#scoreboard的代码
好的,以下是爬取https://www.luogu.com.cn/contest/68651#scoreboard的Python代码:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 构造请求头
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'}
# 发送请求
url = 'https://www.luogu.com.cn/contest/68651#scoreboard'
response = requests.get(url, headers=headers)
# 解析网页
soup = BeautifulSoup(response.text, 'html.parser')
# 获取数据
table = soup.find("table", {"class": "lg-table"})
thead = table.find("thead")
th_list = thead.find_all("th")
columns = [th.text.strip() for th in th_list]
tbody = table.find("tbody")
tr_list = tbody.find_all("tr")
data = []
for tr in tr_list:
td_list = tr.find_all("td")
row_data = [td.text.strip() for td in td_list]
data.append(row_data)
# 保存数据到CSV文件
df = pd.DataFrame(data, columns=columns)
df.to_csv("contest_data.csv", index=False)
```
这段代码会使用requests库向指定URL发送请求,并使用BeautifulSoup库解析返回的HTML代码。我们可以从HTML代码中找到比赛排名表格的相关信息,并使用pandas库将表格数据保存到CSV文件中。
请注意,这段代码中使用了一个User-Agent请求头,这是为了模拟浏览器发送的请求,避免被网站识别为爬虫并屏蔽。如果您需要爬取其他网站,请根据需要修改请求头。