【发布时间】:2019-02-22 18:32:57
【问题描述】:
我是网络抓取的新手,希望从 Target 的网站提取产品数据。
图像的高亮部分
我已经能够获得产品名称和价格,但无法使用 BeautifulSoup 找到其余信息。例如,在检查邮政编码时,它会显示带有 data-test 标签的邮政编码,但在搜索标签时却找不到。有没有人经历过这种情况或知道如何获取这些信息?
使用 Python 3 和 BeautifulSoup。
不确定表达这个问题的最佳方式,所以如果您需要更多信息或者我需要重新表达,请告诉我。
<a href="#" class="h-text-underline Link-sc-1khjl8b-0 jvxzGg" data-test="storeFinderZipToggle">35401</a>
import requests
from bs4 import BeautifulSoup
f = open("demofile.txt", "w")
Page_Source = "https://www.target.com/p/nintendo-switch-with-neon-blue-and-neon-red-joy-con/-/A-52189185"
page = requests.get(Page_Source)
soup = BeautifulSoup(page.content, 'html.parser')
#write all the html code to a file to compare source files
f.write(str(soup))
#should contain city location but Secondary header can't be found
#location = soup.find("div", {'class', 'HeaderSecondary'})
#inside the secondary header should contain the store name but is not found
#store_location = location.find('div', {'data-test': 'storeId-store-name'})
#store_location = location.find('button', {'id': 'storeId-utility-NavBtn'})
#contains the rest of the information interested in
main_container = soup.find(id="mainContainer")
#complete_product_name = soup('span',attrs={'data-test':'product-title'})[0].text
product_price = soup.find("span", {'data-test': 'product-price'})
product_title = soup.find("span", {'data-test': 'product-title'})
flexible_fulfillment = main_container.find('div', {'data-test': 'flexible_fulfillment'})
#test = product_zip.find_all('a')
#example = soup.find_all("div", {'data-test': 'storePickUpType'})
example = soup.findAll('div', attrs={'data-test':'maxOrderQuantityTxt'})
print(product_title)
print(product_price)
print(flexible_fulfillment)
f.close()
【问题讨论】:
-
商店查找器使用 javascript 初始化。您可以尝试使用其他模块,例如
selenium或requests-html
标签: python-3.x web-scraping beautifulsoup