我正在做一个小机器人,应该从网站(ebay)提供信息,并使用splinter和python将其放入列表中。我的第一行代码:

from splinter import Browser
with Browser() as browser:
url = "http://www.ebay.com"
browser.visit(url)
browser.fill('_nkw', 'levis')
button = browser.find_by_id('gh-btn')
button.click()


python - 将数据放入网页列表中(碎片)-LMLPHP
如何使用网页中的信息以红色框列出信息?

像:[[“ Levi Strauss&Co. 513修身直筒Jean Ivory男装SZ”,12.99,0],[“ Levi 501牛仔裤男装原创Levi's Strauss牛仔布直筒裤,71.44,” Now“],[” Levis 501 Button Fly牛仔裤收缩以适合多种尺寸”,[$ 29.99,$ 39.99]]

最佳答案

这不是一个完美的答案,但是应该可以。
首先要安装这两个模块
requestsBS4


  点安装请求
  
  pip安装beautifulsoup4


import requests
import json
from bs4 import BeautifulSoup

#setting up the headers
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Referer': 'https://www.ebay.com/',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.8',
'Host': 'www.ebay.com',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
}
#setting up my proxy, you can disable it
proxy={
'https':'127.0.0.1:8888'
}

#search terms
search_term='armani'

#request session begins
ses=requests.session()

#first get home page so to set cookies
resp=ses.get('https://www.ebay.com/',headers=headers,proxies=proxy,verify=False)

#next get the search term page to parse request
resp=ses.get('https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2374313.m570.l1313.TR12.TRC2.A0.H0.X'+search_term+'.TRS0&_nkw='+search_term+'&_sacat=0',
headers=headers,proxies=proxy,verify=False)


soup = BeautifulSoup(resp.text, 'html.parser')
items=soup.find_all('a', { "class" : "vip" })
price_items=soup.find_all('span', { "class" : "amt" })

final_list=list()

for item,price in zip(items,price_items):
    try:
        title=item.getText()
        price_val=price.find('span',{"class":"bold"}).getText()
        final_list.append((title,price_val))
    except Exception as ex:
        pass

print(final_list)


这是我得到的输出

python - 将数据放入网页列表中(碎片)-LMLPHP

关于python - 将数据放入网页列表中(碎片),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45296338/

10-12 16:55