python爬虫之BeatifulSoup

python爬虫之BeatifulSoup,第1张

#安装
#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.*?)",.*?"prodCat":"(?P.*?)",.*?"lowPrice":"(?P.*?)",.*?'
                  r'"highPrice":"(?P.*?)",.*?"avgPrice":"(?P.*?)",', 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!")

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/876297.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存