Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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/8/python-3.x/19.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 《靓汤》中的进一步精炼列表_Python_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 《靓汤》中的进一步精炼列表

Python 《靓汤》中的进一步精炼列表,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我正在使用BeautifulSoup4从网页上获得各种结果。目前我正在尝试处理一个URL列表,让BS从这些页面中获取一个div,并将其存储在一个新列表中。我在这方面很成功。下一步是获取div的文本结果,但是让它成功地迭代这个列表并进一步细化结果对我来说并不成功 最后,我得到了一个名为dealcomments的列表,在执行dealcomments[I].find('a').text时,我尝试遍历该列表中的索引值。我只是不确定如何进一步处理这个列表来完善它 def getDealComments(d

我正在使用BeautifulSoup4从网页上获得各种结果。目前我正在尝试处理一个URL列表,让BS从这些页面中获取一个div,并将其存储在一个新列表中。我在这方面很成功。下一步是获取div的文本结果,但是让它成功地迭代这个列表并进一步细化结果对我来说并不成功

最后,我得到了一个名为
dealcomments
的列表,在执行dealcomments[I].find('a').text时,我尝试遍历该列表中的索引值。我只是不确定如何进一步处理这个列表来完善它

def getDealComments(deallinks):
dealcomments = []

# Can't modify dealcomments to get plain text :~(
for index,link in enumerate(deallinks):
    source = requests.get(link).text
    soup = BeautifulSoup(source, 'lxml')
    dealcomments.append(soup.find_all('div', class_='posttext'))

    print(len(dealcomments))
return dealcomments
这将成功地生成一个包含80个项目的列表,但我无法找到一种方法来将其细化到文本中

如果我跑步:

print(dealcomments.find('a').text)
回到main函数,它给出了一个错误

下面是我得到的“posttext”div的一个示例:

[<div class="posttext" style="margin:20px 0px 10px 0px;display:block;margin-bottom:50px;"><a href="/j/4/?pid=1494475&amp;lno=1&amp;tid=11000&amp;afsrc=1" target="_blank">Amazon</a> has All-new Kindle Paperwhite Now Waterproof on sale. Shipping is free.
<ul>
<li><a href="/j/4/?pid=1494475&amp;lno=1&amp;tid=11000&amp;afsrc=1" target="_blank">All-new Kindle Paperwhite (8GB)</a> for <b>$89.99</b></li>
<li><a href="/j/4/?pid=1494475&amp;lno=2&amp;tid=11000&amp;afsrc=1" target="_blank">All-new Kindle Paperwhite (32GB)</a> for <b>$114.99</b></li>
<li><a href="/j/4/?pid=1494475&amp;lno=3&amp;tid=11000&amp;afsrc=1" target="_blank">All-new Kindle - Now with a Built-in Front Light</a> <b>$65</b></li>
<li><b>YMMV</b>: 15% off a Kindle Paperwhite + $20 ebook credit with promo code "<strong>READKINDLE00</strong>".
[所有新款Kindle Paperwhite现在都有防水产品出售。免费送货。
  • 89.99美元
  • 114.99美元
  • 65美元
  • YMMV:KindlePaperWhite加上20美元的电子书优惠,优惠代码为“READKINDLE00”。

我正在尝试获取“posttext”分区中的所有可读文本。

请尝试以下代码

def getDealComments(deallinks):
 dealcomments = []

# Can't modify dealcomments to get plain text :~(
 for index,link in enumerate(deallinks):
    source = requests.get(link).text
    soup = BeautifulSoup(source, 'lxml')
    for item in soup.find_all('div', class_='posttext'):
        dealcomments.append(item.text)


 return dealcomments

嗯,您正在试图在列表中查找这似乎很有效,文本似乎正是我所需要的,非常感谢您的帮助!!很高兴帮助您