【问题标题】:How can I only print the link with correct keywords in this code?如何在此代码中仅打印带有正确关键字的链接?
【发布时间】:2017-05-01 17:26:56
【问题描述】:

这是我目前所拥有的,现在我只需要输出与“lion-double-ring”的链接,因为现在空闲打印整个信息页面。这应该循环直到找到链接并使用给定关键字打印链接。也许正则表达式是去这里的方式? 链接

from bs4 import BeautifulSoup
import requests

r = requests.get('walmart.com)
soup = BeautifulSoup(r.text, 'html')
links = soup.find_all('loc')
if "lion" and "double" in str(links):
print str(links)
else:
print('nothing')

【问题讨论】:

  • 这是链接 shop.exclucitylife.com/products/lion-double-ring
  • 谢谢,这会有所帮助:)

标签: python regex web-scraping beautifulsoup lxml


【解决方案1】:
from bs4 import BeautifulSoup
import requests

r = requests.get('http://shop.exclucitylife.com/sitemap_products_1.xml?from=1331122689&to=8543902145')
soup = BeautifulSoup(r.text, 'lxml')
links = soup.find_all('loc')
for link in links:
    if 'lion-double-ring' in link.text:
        print(link.text)
        break
else:
    print('nothing')

【讨论】:

  • 谢谢!您可以查看我上面对原始帖子所做的编辑吗?无法完成电子邮件部分..
  • 对不起,我不熟悉邮件模块。
猜你喜欢
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 2018-02-08
  • 2023-01-11
  • 2022-01-26
  • 2017-12-01
  • 2019-03-08
  • 1970-01-01
相关资源
最近更新 更多