【发布时间】:2021-08-14 11:10:57
【问题描述】:
我的代码有问题。我正在尝试提取本网站 (https://www.local.ch/en/q/geneve/employment%20agency?slot=yellow) 上列出的职位,其中包含公司名称及其信息的链接。第一部分有效,我可以打印所有名称,但是打印到其信息的链接会给我错误:
Traceback (most recent call last):
File "main.py", line 20, in <module>
href = (links.get("href"))
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/bs4/element.py", line 921, in __getattr__
raise AttributeError(
AttributeError: 'NavigableString' object has no attribute 'get'
这是我的代码:
print("Hello, welcome to local job in geneva finder")
import requests
from bs4 import BeautifulSoup
url = "https://www.local.ch/en/q/geneve/employment%20agency?slot=yellow"
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, "html.parser")
names = soup.findAll("h2")
for name in names:
print(name.text)
link = soup.find("a")
for links in link:
href = (links.get("href"))
if href.startswith("https://www.local.ch/en/d/geneve/1204/recruiting"):
print(href)
【问题讨论】:
-
for links in name:- 应该是for links in link:吗?
标签: python web-scraping attributeerror