且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用BeautifulSoup加快解析速度?

更新时间:2023-01-31 08:34:42

 导入请求从bs4导入BeautifulSoup导入json从datetime导入datetime为dt导入csvdef汤(内容):汤= BeautifulSoup(content,'html.parser')回汤def Main(url):r = request.get(URL)汤=汤(r.content)spans = soup.findAll('span',class _ ='fw_bold')链接= [f"{url [:27]} {span.a ['href']}"(跨度为跨度)返回链接def Parent():链接= Main("http://ticket.interpark.com/TPGoodsList.asp?Ca=Liv&SubCa=Fes")使用open("result.csv",'w',newline =",encoding ="utf-8")作为f:writer = csv.writer(f)writer.writerow([["Name","Singers","Location","Date","ImageUrl"])与request.Session()作为要求:对于链接中的链接:r = req.get(链接)汤=汤(r.content)脚本= json.loads(soup.find("script",type ="application/ld + json").text)名称=脚本[名称"]打印(f提取:{name}")歌手=脚本[表演者"] [名称"]位置=脚本[位置"] [名称"]datelist = list(script.values())[3:5]datet = []图片=脚本[图片"]对于日期列表中的日期:日期= dt.strptime(日期,'%Y%m%d').strftime('%d-%m-%Y')datet.append(日期)writer.writerow([姓名,歌手,位置,:" .join(datest),* image])父母() 

运行并检查输出在线

解决方案