Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么.get('href')在bs4.element.tag上返回“None”?_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 3.x 为什么.get('href')在bs4.element.tag上返回“None”?

Python 3.x 为什么.get('href')在bs4.element.tag上返回“None”?,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我正在收集一个数据集进行分析。目标是解析SEC网页上的一个表,并拉出一行中包含文本SC13D的链接。这需要是可重复的,这样我就可以在数据库中的大量链接列表中实现自动化。我知道这段代码不是最具python风格的,但我将其整合在一起,以从表中获取所需内容,除了表行中的链接。如何从表行中提取href值 我尝试在表第15行中的“tr”而不是“td”上使用.findAll,但如果执行.findAll'td',则无法找到如何在SC 13D上搜索并从表行列表中弹出元素。我还尝试使用代码第32行中包含的.get

我正在收集一个数据集进行分析。目标是解析SEC网页上的一个表,并拉出一行中包含文本SC13D的链接。这需要是可重复的,这样我就可以在数据库中的大量链接列表中实现自动化。我知道这段代码不是最具python风格的,但我将其整合在一起,以从表中获取所需内容,除了表行中的链接。如何从表行中提取href值

我尝试在表第15行中的“tr”而不是“td”上使用.findAll,但如果执行.findAll'td',则无法找到如何在SC 13D上搜索并从表行列表中弹出元素。我还尝试使用代码第32行中包含的.get'a而不是.get'href'获取带有链接的锚定标记,但它也不返回任何内容

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = 'https://www.sec.gov/Archives/edgar/data/1050122/000101143807000336/0001011438-07-000336-index.htm'

html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table',{'summary':'Document Format Files'})
rows = table.findAll("td")

i = 0
pos = 0
for row in rows:
    if "SC 13D" in row:
        pos = i
        break
    else: i = i + 1

linkpos = pos - 1

linkelement = rows[linkpos]

print(linkelement.get('a'))
print(linkelement.get('href'))
预期结果是打印linkelement中的链接。实际结果是无。

切换您的.get to.find

您希望找到标记,并打印href属性

 print(linkelement.find('a')['href'])
或者您需要使用。使用标签获取:

print(linkelement.a.get('href'))
这是因为你的a标签在你的td标签里面 你只需要做:

linkelement = rows[linkpos]
a_element = linkelement.find('a')

print(a_element.get('href'))