Loading... # python爬取菜价 就附个简单bs4使用 ~~~python import requests from bs4 import BeautifulSoup url = "http://www.bjtzh.gov.cn/bjtz/home/jrcj/index.shtml" resp = requests.get(url) # 页面源代码交给BeautifulSoup进行处理,生成bs对象 resp.encoding = 'UTF-8' # 爬取网页先看是什么编码 page = BeautifulSoup(resp.text, "html.parser") # 指定HTML解析器 table = page.find("table", attrs={"width": "588"}) trs = table.find_all("tr") for tr in trs: try: tds = tr.find_all("td") name = tds[0].text kind = tds[1].text high = tds[2].text avg = tds[3].text except IndexError as e: continue # 因为网站有些表格不规范,直接抛出错误 print(name, kind, high, avg) ~~~ 最后修改:2022 年 12 月 29 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏