我正在尝试使用bs4从页面中的所有项目中获取标题标签,然后打印出所有标题。如果我做 print(soup.find(“a”,attrs = {“class”:“detLink”})[“title”])我只得到其中一个的标题。如果我将“select”切换为findAll或find_all,我会收到错误消息:
print(soup.findAll("a", attrs={"class": "detLink"})["title"])
TypeError: list indices must be integers or slices, not str
这是我的代码:
def test():
url_to_scrape = "https://test.com"
r = requests.get(url_to_scrape)
soup = BeautifulSoup(r.text, "html5lib")
print(soup.select("a", attrs={"class": "detLink"})["title"])
test()
如何获得所有项目的标题?
答案 0 :(得分:0)
请尝试:
def test():
url_to_scrape = "https://test.com"
r = requests.get(url_to_scrape)
soup = BeautifulSoup(r.text, "html5lib")
titles = [div["title"]
for div in soup.find_all("a", attrs={"class": "detLink"})]
print(titles)
test()
有效地使用列表的列表理解。