如何检索网页的链接并使用Python复制链接的URL地址?
我想使用从Google Arts & CultureBeautifulSoup
检索信息。我检查了许多 stackoverflow 帖子(、、、、、
)
[1]
,
但
仍然无法检索到信息。[2]
[3]
[4]
[5]
但是,我想要每个图块(图片)的 ( li
) 信息,例如 href,find_all
并select one
返回空列表或无。
您能帮我获取“e0WtYb HpzMff PJLMUc”类锚标记的以下 href 值吗?
href="/entity/claude-monet/m01xnj?categoryId=artist"
以下是我尝试过的。
import requests
from bs4 import BeautifulSoup
url = 'https://artsandculture.google.com/category/artist?tab=time&date=1850'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
print(soup.find_all('li', class_='DuHQbc')) # []
print(soup.find_all('a', class_='PJLMUc')) # []
print(soup.find_all('a', class_='e0WtYb HpzMff PJLMUc')) # []
print(soup.select_one('#tab_time > div > div:nth-child(2) > div > ul > li:nth-child(2) > a')) # …
Run Code Online (Sandbox Code Playgroud) 我知道这个问题在这个网站上被问了很多,但我已经尝试了所有给出的解决方案,但我不知道如何解决这个问题。
对于初学者:我在Windows 10
使用Python 3.6
. 我安装Anaconda
为我的 IDE。
我尝试BeautifulSoup4
使用pip
install 安装beautifulsoup4
,但我得到了
已满足要求
回复。
我试图运行的代码只是
from bs4 import BeautifulSoup4
to which I get the error: ImportError: cannot import name 'BeautifulSoup4'
The full error is:
runfile('C:/Users/ter/.spyder-py3/untitled1.py', wdir='C:/Users/ter/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-21-8717178e85e1>", line 1, in <module>
runfile('C:/Users/ter/.spyder-py3/untitled1.py', wdir='C:/Users/ter/.spyder-py3')
File "C:\Users\ter\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\ter\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/ter/.spyder-py3/untitled1.py", line 8, in …
Run Code Online (Sandbox Code Playgroud) 我需要从链接(a)标签中取"href"属性.
我跑
label_tag = row.find(class_='Label')
print(label_tag)
Run Code Online (Sandbox Code Playgroud)
我得到了(抱歉,出于隐私原因,我无法显示链接和文字)
<a class="Label" href="_link_">_text_</a>
Run Code Online (Sandbox Code Playgroud)
的类型
<class 'bs4.element.Tag'>
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时(如BeautifulSoup所示,获得href)
tag_link = label_tag['href']
print(tag_link)
Run Code Online (Sandbox Code Playgroud)
我想以下错误(在第一个命令上)
TypeError: 'NoneType' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
任何线索?提前致谢
[已解决]编辑:我犯了一个错误(循环使用异构结构的元素)
python ×4
anaconda ×1
html ×1
html-parsing ×1
hyperlink ×1
python-3.x ×1
web-crawler ×1
web-scraping ×1