尝试从网站上抓取文字时,只能连接列表(而不是“ str”)来列出

时间:2019-04-03 12:14:54

标签: python web-scraping lxml discord

当我尝试从网站抓取中提取文本时,我不断收到错误消息,

我试图获得 itemName =(用于文本) pic =(链接到图片)

我正在使用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&amp;odnWidth=450&amp;odnBg=FFFFFF" alt="Funko POP! Marvel: Avengers Endgame -&nbsp;Ronin">

元素试图从以下位置提取文本的示例:(content =)

<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>

我的代码:

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='@')

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

0 个答案:

没有答案