下面的脚本旨在浏览 ebay 搜索页面上的 ebay 列表。搜索页面只是一个列表,所以我试图遍历每个 li 标签并将内容添加到变量中。由于某种原因,这个脚本似乎不想工作,我不知道为什么。
from urllib.request import urlopen
from bs4 import BeautifulSoup
# specify the url
url = "https://www.ebay.co.uk/sch/i.html?_from=R40&_nkw=funko+gamora+199&_sacat=0&LH_Sold=1&LH_Complete=1&rt=nc&LH_PrefLoc=1&_ipg=200"
# Connect to the website and return the html to the variable ‘page’
try:
page = urlopen(url)
except:
print("Error opening the URL")
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page, 'html.parser')
# Take out the <div> of name and get its value
content = soup.find('ul', {"class": "srp-results srp-list clearfix"})
#print(content)
article = ''
for i in content.findAll('li'):
article = article + ' ' + i.text
print(article)
# Saving the scraped text
with open('scraped_text.txt', 'w') as file:
file.write(article)
谁能看到我哪里出错了?