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
Python 3.x 无法从HTML BeautifulSoup对象提取URL_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 3.x 无法从HTML BeautifulSoup对象提取URL

Python 3.x 无法从HTML BeautifulSoup对象提取URL,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我希望从名为url的html BeautifulSoup对象中提取以下url,如下所示: []试试这个。下面是一个例子: find_all已经为您提供了一个元素列表;你只需要从每个人那里获得href from bs4 import BeautifulSoup import requests url = 'https://mania.bg/p/pulover-alexander-mcqueen-p409648' r = requests.get(url) soup = BeautifulSo

我希望从名为url的html BeautifulSoup对象中提取以下url,如下所示:


[]试试这个。下面是一个例子:

find_all已经为您提供了一个元素列表;你只需要从每个人那里获得href

from bs4 import BeautifulSoup
import requests

url = 'https://mania.bg/p/pulover-alexander-mcqueen-p409648'

r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')

for a in soup.find_all(
        'a',
        attrs={'class':
               'product sellout product-sellout float-left status-1'}):
    print(a['data-producturl'])

正确,我正在编辑这个问题,因为这个代码soup.find_all'a',attrs={'class':'product sellout product sellout float left status-1}实际上返回列出的html。我需要从中提取url。根据新的要求编辑我的答案。如果仍然没有提供所需的数据,那么您需要定义所需的url。代码现在看起来像这样,工作起来像一个符咒:url=csoup。find_all'a',attrs={'class':'product sellout product sellout float left status-1',href=True for num in RangeURL:url=url[num-1]['href']谢谢你的回答,为了避免添加另一个包,我真的会撒谎。另一个答案我已经找到了,没关系^---^
[('https://mania.bg/p/pulover-alexander-mcqueen-p409648', 'https://mania.bg/p/pulover-alexander-mcqueen-p409648')]
from bs4 import BeautifulSoup
import requests

url = 'https://mania.bg/p/pulover-alexander-mcqueen-p409648'

r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')

for a in soup.find_all(
        'a',
        attrs={'class':
               'product sellout product-sellout float-left status-1'}):
    print(a['data-producturl'])
import requests
import bs4

url = 'https://mania.bg/p/pulover-alexander-mcqueen-p409648'
data = requests.get(url)

soup = bs4.BeautifulSoup(data.text,'html.parser')

urls = soup.find_all('a', attrs={'class': 'product sellout product-sellout float-left status-1'})
for num in range(len(urls)):
    url = urls[num]['href']
    print(url)