#安装
#pip install bs4
#1.拿到页面源代码
#2.使用bs4进行解析,拿到数据
import requests
from bs4 import BeautifulSoup
import csv
import re
url = "http://www.xinfadi.com.cn/getPriceData.html"
resp = requests.get(url)
resp.close()
# print(resp.text)
f = open("菜价.csv", mode="w",encoding='utf-8')
csvwriter = csv.writer(f)
#解析数据
#1.把页面源代码交给BeautifulSoup进行处理,生成bs对象
# page = BeautifulSoup(resp.text, "html.parser")#指定html解析器
obj1 = re.compile(r',"prodName":"(?P<name>.*?)",.*?"prodCat":"(?P<type>.*?)",.*?"lowPrice":"(?P<lowPrice>.*?)",.*?'
r'"highPrice":"(?P<highPrice>.*?)",.*?"avgPrice":"(?P<avgPrice>.*?)",', re.S)
result1 = obj1.finditer(resp.text)
for it in result1:
dic = it.groupdict()
csvwriter.writerow(dic.values())
f.close()
print("over!")
#2.从bs对象种查找数据
#find(标签,属性=值)
#find_all(标签,属性=值)
# table = page.find("table","class_="hq_table") #加_因为class是python的关键字
# table = page.find("table",attrrs={"class": hq_table"}) #加_因为class是python的关键字
# print(page.text)
#拿到所有数据行
# trs = table.find_all("tr")[1:]
# for tr in trs: #每一行
# tds = tr.find_all("td") #拿到每行中的所有td
# name = tds[0].text #.text表示拿到被标签所标记的内容
# type = tds[1].text
# max = tds[2].text
# min = tds[3].text
# avg = tds[4].text
# csvwriter.writerow([name,type,max,min,avg])
#
# f.close()
# print("over!")
python爬虫之BeatifulSoup
最新推荐文章于 2023-01-31 18:09:03 发布