Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.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 如何获得';href';从使用BeautifulSoup的html标记_Python_Html_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 如何获得';href';从使用BeautifulSoup的html标记

Python 如何获得';href';从使用BeautifulSoup的html标记,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,我试图从一个表中提取一个图像链接,并且已经到达了“td”标记的点,但是无法获取其中的链接。 这是我的密码: 从bs4导入美化组 导入请求 def get_html(url): r=请求。获取(url) r、 编码='utf8' 返回r.text 数据=“” ''' def get_dt(html): soup=BeautifulSoup(html,“lxml”) a=soup.findAll('table')[1].findAll('tr')) 对于范围内的tr(len(a)): b=a[tr]

我试图从一个表中提取一个图像链接,并且已经到达了“td”标记的点,但是无法获取其中的链接。 这是我的密码:

从bs4导入美化组
导入请求
def get_html(url):
r=请求。获取(url)
r、 编码='utf8'
返回r.text
数据=“”
'''
def get_dt(html):
soup=BeautifulSoup(html,“lxml”)
a=soup.findAll('table')[1].findAll('tr'))
对于范围内的tr(len(a)):
b=a[tr].findAll('td')
对于范围内的td(len(b)):
如果tr==0和td==0:
c=b[td]
打印(c.get('href'))
def get_dt2(html):
soup=BeautifulSoup(html,“lxml”)
打印(soup.get('href'))
#链接http://www.rech-deti.ru/catalog/7/61021/'
获取dt2(数据)
我不断得到输出:

或者如果我使用

soup['href']
我得到:

回溯(最近一次呼叫最后一次):
文件“C:/Users/Vlad/PycharmProjects/Ultimate_Parser/Rech/Rech table test.py”,第42行,在
获取dt2(数据)
文件“C:/Users/Vlad/PycharmProjects/Ultimate_Parser/Rech/Rech table test.py”,第38行,在get_dt2中
打印(soup['href'])
文件“C:\Users\Vlad\PycharmProjects\Ultimate\u Parser\venv\lib\site packages\bs4\element.py”,第1401行,在u getitem中__
返回self.attrs[键]
KeyError:'href'
我试着使用这个问题的答案:
但是,它们都不起作用。

尝试此操作以获取包含href属性的所有a元素:

def get_dt2(html):
    soup = BeautifulSoup(html, 'lxml')
    for a in soup.find_all('a', href=True):
        print (a['href'])