【发布时间】:2021-07-13 00:29:44
【问题描述】:
我正在尝试使用 bs4 解析 g2g.com 页面。但没有什么对我有用。代码中一定有错误。可能是什么原因?提前致谢!
import requests
from bs4 import BeautifulSoup
link = "https://www.g2g.com/offer-11677180/Amnennar-TBC--FR----Alliance?service_id=lgc_service_1&brand_id=lgc_game_29076®ion_id=ac3f85c1-7562-437e-b125-e89576b9a38e&sort=lowest_price&attributes=%257B%2522lgc_29076_server%2522%253A%255B%2522lgc_29076_server_40957%2522%255D%257D&online=online"
response = requests.get(link)
with open(path + str(fileName) + ".html", "w") as f:
f.write(response.text)
with open(path + str(fileName) + ".html", "r") as f:
html = f.read()
with open(path + str(fileName) + ".html", "w") as f:
soup = BeautifulSoup(html, "html.parser")
blockList = soup.find("div", {"id": "pre_checkout_sls_offer"})
for i in blockList:
block = blockList.find_all("div", {"class": "other_offer-desk-main-box other_offer-div-box"})
for j in block:
insideBlock = block.find_all("div", {"class": "flex-1 align-self"})
for k in insideBlock:
seller = insideBlock.find("div", class_="seller__name-detail")
stock = insideBlock.find("div", class_="offers-bottom-attributes offer__content-lower-items")("span") # str(stock)[7:-8]
price = insideBlock.find("span", class_="offer-price-amount")
print(seller, stock, price)
【问题讨论】:
-
请告诉我们有什么问题,包括您遇到的任何错误,以及您期望的输出。
-
@JanWilamowski 我想获取一个包含产品信息的字符串。一页显示 20 个结果。但问题是它只向我显示了一个 20 次。
-
这是一个很好的问题,只是一件小事,你必须edit你的问题,并包括你的预期输出和当前输出,以便我们双方更轻松!
标签: python-3.x parsing beautifulsoup