【问题标题】:web scraping with lxml to discord, how do i pull text and photo links?使用 lxml 抓取网页到不和谐,我如何提取文本和照片链接?
【发布时间】:2019-08-24 09:19:47
【问题描述】:

我正在使用 lxml 从一堆零售商那里提取产品,并将信息发送到不和谐的服务器。 我已经完成了大部分工作,除了抓取链接(图片等:https://i5.walmartimages.com/asr/b05aca03-968b-4e43-8b48-781aa52ef3e0_1.7fa663da8e1c382271e6649007e9a60b.png?odnHeight=450&odnWidth=450&odnBg=FFFFFF

和文本,以获取商品名称和价格。

这是嵌入不和谐 webhook 的外观图片

我正在尝试抓取的产品:https://www.walmart.com/ip/POP/295232252

我试图从以下位置拉取图像的元素示例:(src=)

<img class="hover-zoom-hero-image" src="https://i5.walmartimages.com/asr/b05aca03-968b-4e43-8b48-781aa52ef3e0_1.7fa663da8e1c382271e6649007e9a60b.png?odnHeight=450&odnWidth=450&odnBg=FFFFFF" alt="Funko POP! Marvel: Avengers Endgame - Ronin">

我试图从中提取文本的元素示例:(content=)

<h1 class="prod-ProductTitle no-margin font-normal heading-a" itemprop="name" content="Funko POP! Marvel: Avengers Endgame - Ronin"><div>Funko POP! Marvel: Avengers Endgame - Ronin</div></h1>

我的代码:

def send_embed(link):
embed = Embed(
description=link,
color=color_embed,
timestamp='now' # sets the timestamp to current time
)
image1 = 'https://www.blakleysflooring.com/wp-content/uploads/2016/03/Placeholder.png'
image2 = pic

embed.set_author(name=itemName + "|" + price, url=link)
embed.add_field(name='Retailer', value=site)
embed.add_field(name='Stock', value=instock)
embed.set_footer(text='@QuantumPings')

embed.set_thumbnail(image2)
hook.send(embed=embed)


def getDatetime():
return '[{}]'.format(str(datetime.datetime.now())[:-3])


def monitor(link):
global embed
global itemName
global pic
global site
global instock
global price
global color_embed
try:
with session as s:
r = s.get(link, timeout=10)
r.raise_for_status()

tree = etree.HTML(r.content)
if 'walmart' in link:
oos = False if tree.xpath('//span[@class="button-wrapper"]') else True
site = 'Walmart'
instock = False if tree.xpath('/html/body/div[2]/div/div/div[2]/div/div[1]/div/div[1]/div/div/div/div/div[3]/div[4]/div[2]/div[1]/div/div/div/div[4]/div/div/div[2]/div/div/div/span') else False
pic = ()
itemName = tree.xpath('//div[@class="ProductTitle"]/h2/div/text()')
price = '$0.00'
color_embed = 0x00f714 if instock is True else 0xff0008
 True else 0xff0008

【问题讨论】:

  • 您可以使用BeautifulSoup 代替吗?我没有使用lxml,但使用BeautifulSoup 解析文本是ez-pz
  • @Reedinationer id 不喜欢,但如果归根结底,我想我可以。

标签: python lxml discord


【解决方案1】:

这是一个如何使用lxml 收集文本/链接的基本示例。您几乎可以使用lxml 做任何事情,所以请务必进行实验。另外,我认为lxmlBeautifulSoup 更快,但它并不优雅。我也会阅读https://lxml.de/lxmlhtml.html

from lxml import html

response = """
<h1 class="prod-ProductTitle no-margin font-normal heading-a" itemprop="name" content="Funko POP! Marvel: Avengers Endgame -&nbsp;Ronin"><div>Funko POP! Marvel: Avengers Endgame -&nbsp;Ronin</div></h1>
<img class="hover-zoom-hero-image" src="https://i5.walmartimages.com/asr/b05aca03-968b-4e43-8b48-781aa52ef3e0_1.7fa663da8e1c382271e6649007e9a60b.png?odnHeight=450&amp;odnWidth=450&amp;odnBg=FFFFFF" alt="Funko POP! Marvel: Avengers Endgame -&nbsp;Ronin">
"""

tree = html.fromstring(response)

title = [e for e in tree.cssselect('h1') if e.get('itemprop') is not None and e.get('itemprop') == 'name']
title = title[0]
title = title.text_content()
print (title)

img = tree.cssselect('img.hover-zoom-hero-image')
img = img[0]
img = img.get('src')
print (img)

【讨论】:

  • 问题是我需要它来处理很多链接,我不想为每个产品单独放置元素。 itemName= 目前对我不起作用,我收到错误“只能在我的设置中将列表(而不是“str)连接到列表”
  • 我使用的是itemprop而不是itemname?如果您一次请求多个网址,请使用aiohttp 之类的内容,但这不会改变您收集title / link 的方式。
猜你喜欢
  • 2021-12-25
  • 2011-01-06
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 2018-01-21
  • 1970-01-01
相关资源
最近更新 更多