Webscraping URL构造

2024-06-08 14:04:43 发布

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

考虑URL:

https://en.wikipedia.org/wiki/NGC_2808

当我在temp = requests.get(url).text中将它直接用作我的url时,一切正常。你知道吗

现在,考虑字符串name = NGC2808。现在,当我做s = name[:3] + '_' + name[3:]然后再做url = 'https://en.wikipedia.org/wiki/' + s ,程序不工作了。你知道吗

这是代码段:

s = name[:3] + '_' + name[3:]
url0 = 'https://en.wikipedia.org/wiki/' + s

url = requests.get(url0).text
soup = BeautifulSoup(url,"lxml")
soup.prettify()

table = soup.find('table',{'class':'infobox'})
tags = table.find_all('tr')

错误如下: AttributeError: 'NoneType' object has no attribute 'find_all'

编辑: 这个名称并没有明确定义为"NGC2808",而是来自于扫描一个.txt文件。但是print(name)会导致NGC2808。现在,当我直接提供名称,而不扫描文件时,我没有得到任何错误。为什么会这样?你知道吗

为什么会这样?你知道吗


Tags: textnamehttpsorgurlgetwikitable
2条回答

如果只在从文件源读取时发生,则名称中必须有一些特殊(Unicode)或空白字符,如果您使用的是PyCharm,那么您可以进行一些调试,或者使用pprint()repr()方法打印名称字符串(仅在从文件中读取名称字符串之后),以查看导致该问题的字符,让我们举一个示例代码,其中normalprint函数不会显示特殊字符,但pprint会显示。。。你知道吗

from bs4 import BeautifulSoup
from pprint import pprint
import requests

# Suppose this is a article id fetched from the file
article_id = "NGC2808   "

# Print will not show any special character
print(article_id)

# Even you can print this special character using repr() method
print(repr(article_id))

# Pprint shows a the character code in place of special character
pprint(article_id)

# Now this code will produce an error
article_id_mod = article_id[:3] + '_' + article_id[3:]
url = 'https://en.wikipedia.org/wiki/' + article_id_mod

response = requests.get(url)
soup = BeautifulSoup(response.text,"lxml")

table = soup.find('table',{'class':'infobox'})
if table:
    tags = table.find_all('tr')
    print(tags) 

现在要解决同样的问题,您可以执行以下操作:

  • 如果字符串的开头/结尾有多余的空格,请使用strip()方法

    article\u id=物品_id条()

  • 如果有特殊字符:使用适当的正则表达式,或使用vscode/sublime/notepad++等编辑器打开文件,然后使用find/replace选项。

提供一个minimal reproducible example和一个错误消息的副本在这里会有很大的帮助,并且可能会对您的问题有更深入的了解。你知道吗

不过,以下几点对我很有用:

name = "NGC2808"
s = name[:3] + '_' + name[3:]
url = 'https://en.wikipedia.org/wiki/' + s
temp = requests.get(url).text
print(temp)

由于问题更改而编辑:

您提供的错误表明beautiful soup在get请求返回的文档中找不到任何表。您是否检查了传递给该请求的url以及返回的内容?你知道吗

从目前的情况来看,我可以得到一个标签列表(如您所希望的),包括以下内容:

import requests
from bs4 import BeautifulSoup
import lxml

name = "NGC2808"
s = name[:3] + '_' + name[3:]
url = 'https://en.wikipedia.org/wiki/' + s
temp = requests.get(url).text
soup = BeautifulSoup(temp,"lxml")
soup.prettify()

table = soup.find('table',{'class':'infobox'})
tags = table.find_all('tr')
print(tags)

s = name[:3] + '_' + name[3:]的缩进方式很奇怪,这表明示例顶部缺少细节。有这样的上下文可能会很有用,因为不管涉及什么逻辑,都会导致向get请求传递一个格式错误的url。你知道吗

相关问题 更多 >