【问题标题】:HTTP Error 404: Not Found - BeautifulSoup and PythonHTTP 错误 404:未找到 - BeautifulSoup 和 Python
【发布时间】:2018-05-27 21:51:45
【问题描述】:

我有一个抓取网站的脚本,但我不断收到“urllib.error.HTTPError:HTTP 错误 404:未找到”。我尝试将用户代理添加到标头并运行脚本,但仍然遇到相同的错误。这是我的代码

from urllib.request import urlopen, Request
from bs4 import BeautifulSoup as soup
import json

atd_url = 'https://courses.lumenlearning.com/catalog/achievingthedream'

#opening up connection and grabbing page
res = Request(atd_url,headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
uClient = urlopen(res)
page_html = uClient.read()
uClient.close()

#html parsing
page_soup = soup(page_html, "html.parser")

#grabs info for each textbook
containers = page_soup.findAll("div",{"class":"book-info"})

data = []
for container in containers:
   item = {}
   item['type'] = "Course"
   item['title'] = container.h2.text
   item['author'] = container.p.text
   item['link'] = container.p.a["href"]
   item['source'] = "Achieving the Dream Courses"
   item['base_url'] = "https://courses.lumenlearning.com/catalog/achievingthedream"
   data.append(item) # add the item to the list

with open("./json/atd-lumen.json", "w") as writeJSON:
   json.dump(data, writeJSON, ensure_ascii=False)

这是我每次运行脚本时收到的完整错误消息

Traceback (most recent call last):
File "atd-lumen.py", line 9, in <module>
uClient = urlopen(res)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 532, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 570, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

关于如何解决此问题的任何建议?当输入浏览器时,它是一个有效的链接。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    改用 requests 库,这可行:

    import requests
    
    #opening up connection and grabbing page
    response = requests.get(atd_url,headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
    
    #html parsing
    page_soup = soup(response.content, "html.parser")
    

    【讨论】:

      猜你喜欢
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2020-11-21
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多