【发布时间】:2019-12-30 05:59:54
【问题描述】:
我正在尝试从 GoT wiki 页面中提取图片链接 前两个链接可以找到,但后两个给我一个 404 错误代码。 我试图找出我做错了什么。
我尝试了不同的组合来找到正确的链接。
import requests
from bs4 import BeautifulSoup
import urllib
import urllib.request as request
import re
url = 'https://en.wikipedia.org/w/index.php' + \
'?title=List_of_Game_of_Thrones_episodes&oldid=802553687'
r = requests.get(url)
html_contents = r.text
soup = BeautifulSoup(html_contents, 'html.parser')
# Find all a tags in the soup
for a in soup.find_all('a'):
# While looping through the text if you find img in 'a' tag
# Then print the src attribute
if a.img:
print('http:/'+a.img['src'])
# And here are the images on the page
http:///upload.wikimedia.org/wikipedia/en/thumb/e/e7/Cscr-featured.svg/20px-Cscr-featured.svg.png
http://static/images/wikimedia-button.png
http://static/images/poweredby_mediawiki_88x31.png
前两个链接有效
但我想让后两个链接也能正常工作。
【问题讨论】:
-
这些链接在网络浏览器中也给了我 404。你是怎么得到这些链接的?也许他们在请求中需要一些标头 - 即。
Referer或User-Agent. -
网址是相对的 - 您必须在开头添加
https://en.wikipedia.org/才能获得完整的网址,例如https://en.wikipedia.org/static/images/wikimedia-button.png
标签: python python-3.x beautifulsoup jupyter-notebook