【问题标题】:beautifulsoup get href out of unsorted listbeautifulsoup 从未排序的列表中获取 href
【发布时间】:2016-07-10 19:45:39
【问题描述】:

为什么这段代码没有输出三个链接到搜索引擎找到的三个点击?

from bs4 import BeautifulSoup

from urllib import urlopen

import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
url = "https://www.conrad.de/de/Search.html?search=mosfet+driver"
page = opener.open(url)
soup = BeautifulSoup(page, "html5lib")
#print soup.prettify


result = soup.find_all('ul', class_="ccpProductList")

products  = result[0].find_all('a', class_="ccpProductListItem__title")

for product in products:
    print product.href

输出为“无”三倍。

【问题讨论】:

标签: bs4


【解决方案1】:

我认为您的问题是最后一行print product.href。我认为您需要print product['href']。下面的代码做我认为你想要的:

from bs4 import BeautifulSoup
import urllib2

url = 'https://www.conrad.de/de/Search.html?search=mosfet+driver'
req = urllib2.Request(url)
response = urllib2.urlopen(req)
page = response.read()
soup = BeautifulSoup(page,'html.parser')
#print soup.prettify

result = soup.find_all('ul', class_="ccpProductList")
products = result[0].find_all('a', class_="ccpProductListItem__title")
for p in products:
    print p['href']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2015-02-27
    相关资源
    最近更新 更多