# 1 拿到页面源代码
# 2 使用bs4进行解析,拿到数据
import requests
from bs4 import BeautifulSoup
import csv
url = "http://www.xinfadi.com.cn/priceDetail.html"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.12151 SLBChan/30"
}
resp = requests.get(url, headers=headers).content
f = open("菜价.csv", mode="w")
csvwriter = csv.writer(f)
# 解析数据
# 1. 把页面源代码交给BeautifulSoup进行处理,生成bs对象
page = BeautifulSoup(resp.text, "html.parser") # 指定html解析器
# 2 从bs对象中查找数据
# find(标签,属性=值)
# find_all(标签,属性=值)
table = page.find("thead") # class是python的关键字
print(table)
# 拿到所有数据行
csvwriter.writerow([table])
trs = table.find_all("tr")[1:]
for tr in trs: # 每一行
ths = tr.find_all("th") # 拿到每行中的所有td
name = ths[0].text # .text 表示拿到被标签标记的内容
low = ths[1].text
avg = ths[2].text
high = ths[3].text
gui = ths[4].text
kind = ths[5].text
date = ths[6].text
csvwriter.writerow([name, low, high, gui, kind, date])
f.close()
resp.close()
print("over!!!!")
10-28
131
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-21
11-25
335
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-30