我一直在使用urllib.Request时收到HTTP 400错误请求错误?

2024-10-04 03:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我一直坐在这个问题有一点现在,我不断得到一个坏的请求错误时,运行下面的代码

url = input("Twitter link: ")
print("\n")
html_doc = urllib.request.urlopen(url)
soup = BeautifulSoup(html_doc, 'lxml')

name = soup.find('h1').a.text
location = soup.find('span', {'class' : 'ProfileHeaderCard- 
locationText'}).text
locationstrip = location.strip()
created = soup.find('span', {'class' : 'ProfileHeaderCard- 
joinDateText'}).text
birthday = soup.find('span', {'class' : 'ProfileHeaderCard- 
birthdateText'}).text
birthdaystrip = birthday.strip()
posted = soup.find('a', {'class' : 'PhotoRail-headingWithCount'}).text
postedstrip = posted.strip()

print("Info")
print("-------- \n")
print(name)
print(locationstrip)
print(created)
print(birthdaystrip)
 print(postedstrip)
url = "http://www.wikipedia.com/wiki/" + name
formedurl = urllib.request.Request(url, headers={'User-Agent': 'Chrome/70.0.3538.102'})
html_doc = urllib.request.urlopen(formedurl)
soup = BeautifulSoup(html_doc, 'lxml')

我读到你需要指定一个用户代理,所以我做了,使它看起来像一个合法的http请求,但我仍然得到这个错误。提前谢谢


Tags: textnameurldocrequesthtml错误find
2条回答

您需要在name中用未得分的_替换space

name = name.replace(' ', '_')
url = "http://www.wikipedia.com/wiki/" + name

从浏览器中复制标题并逐个删除,直到找到最合适的标题集

相关问题 更多 >