【发布时间】:2020-07-28 17:20:12
【问题描述】:
我正在尝试从该网站获取一些数据
https://www.walmart.com/store/2141-philadelphia-pa/search?query=ice%20cream
我一直在使用这种方法来获取 javascript 加载的网站
def getLocalStoreProducts():
session = requests.Session()
localStoreUrl = 'https://www.walmart.com/store/2141-philadelphia-pa/search?query='
searchWord = "ice cream"
searchWord1 = checkForSpace(searchWord)
wordUrl = localStoreUrl+searchWord1
print(wordUrl)
# try:
categorySoup = BeautifulSoup(session.get(wordUrl).text, 'html.parser')
categorytagId = find_tag(categorySoup)
print("this is the tag id ", categorytagId)
categoryscript = categorySoup.find("script", {"id":categorytagId})
categorydata = json.loads(categoryscript.get_text(strip=True))
filename20 = "se.json"
with open(filename20, "w") as f20:
json.dump(categorydata, f20)
print("saved to file")
getLocalStoreProducts()
这是我的查找标签
def find_tag(soup):
script = soup.find('script', {'type': 'application/json', 'id':re.compile(r'^((?!tb-djs).)*$')})
return script['id']
但我不断收到此错误
TypeError: 'NoneType' 对象不可下标
我如何从这个 url 获取数据
https://www.walmart.com/store/2141-philadelphia-pa/search?query=ice%20cream
【问题讨论】: