python - 为什么我的链接提取不起作用?

标签 python html beautifulsoup concatenation nonetype

我正在学习 Beautiful Soup,并尝试从页面 http://www.popsci.com 中提取所有链接...但我收到语法错误。

此代码应该可以工作,但不适用于我尝试使用的任何页面。我试图找出它不起作用的确切原因。

这是我的代码:

from BeautifulSoup import BeautifulSoup
import urllib2

url="http://www.popsci.com/"

page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

sci=soup.findAll('a')

for eachsci in sci:
    print eachsci['href']+","+eachsci.string

...这是我收到的错误:

Traceback (most recent call last):
  File "/root/Desktop/3.py", line 12, in <module>
    print eachsci['href']+","+eachsci.string
TypeError: coercing to Unicode: need string or buffer, NoneType found
[Finished in 1.3s with exit code 1]

最佳答案

a 元素不包含文本时,eachsci.stringNone - 并且您无法连接 None code> 与使用 + 运算符的字符串,正如您尝试做的那样。

如果将 eachsci.string 替换为 eachsci.text,该错误即可解决,因为 eachsci.text 包含空字符串 ''a 元素为空时,并且将其与另一个字符串连接没有问题。

但是,当您点击没有 href 属性的 a 元素时,您会遇到另一个问题 - 当发生这种情况时,您将得到 KeyError .

您可以使用 dict.get() 来解决这个问题,如果键不在字典中,它能够返回默认值(a 元素假装是字典,所以这有效)。

将所有这些放在一起,以下是有效的 for 循环的变体:

for eachsci in sci:
    print eachsci.get('href', '[no href found]') + "," + eachsci.text

关于python - 为什么我的链接提取不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18289295/

相关文章:

python - 使用 Python 从电子商务 Ajax 站点抓取 JSON 数据

python - wxpython GUI运行命令行程序

python - Azure Databricks - 将 Parquet 文件读取到 DataFrame 中

python - 如何使用 Python CouchDb 库创建或保存方法将现有属性用作 doc_id?

浏览器的 HTML CSS 提示

javascript - jQuery Mobile 如何检测刷新

python - 右对齐包含泰语字符的字符串

javascript - HTTP 响应中的意外内容

python - 如何从 beautifulsoup (Python) 中的表中删除列

python web scrape 具有递增的 id