刮网时如何使用FindAll

2024-04-25 07:40:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想刮https://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw=xbox&_pgn=2&_skc=50&rt=nc并得到瓷砖(微软xbox360 E 250 GB黑色控制台,微软xboxone S 1TB白色带有2个无线控制器等)。在适当的时候,我想给Python脚本提供不同的eBay URL,但是为了这个问题,我只想关注一个特定的eBay URL。你知道吗

然后我想把它们的标题添加到一个数据框中,我将把它写到Excel中。我想我可以自己做这部分。你知道吗

不起作用-

for post in soup.findAll('a',id='ListViewInner'):
    print (post.get('href'))

不起作用-

for post in soup.findAll('a',id='body'):
      print (post.get('href'))

不起作用-

for post in soup.findAll('a',id='body'):
   print (post.get('href'))

h1 = soup.find("a",{"class":"lvtitle"})
print(h1)

不起作用-

for post in soup.findAll('a',attrs={"class":"left-center"}):
    print (post.get('href'))

不起作用-

for post in soup.findAll('a',{'id':'ListViewInner'}):
    print (post.get('href'))

这给了我网页错误部分的链接,我知道href是超链接,而不是标题,但我想如果下面的代码已经工作,我可以修改它的标题-

for post in soup.findAll('a'):
    print (post.get('href'))

这是我所有的密码-

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import urllib.request
from bs4 import BeautifulSoup

#BaseURL, Syntax1 and Syntax2 should be standard across all
#Ebay URLs, whereas Request and PageNumber can change 

BaseURL = "https://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw="

Syntax1 = "&_skc=50&rt=nc"

Request = "xbox"

Syntax2  = "&_pgn="

PageNumber ="2"

URL = BaseURL + Request + Syntax2 + PageNumber + Syntax1


print (URL)
HTML = urllib.request.urlopen(URL).read()

#print(HTML)

soup=b(HTML,"html.parser")

#print (soup)

for post in soup.findAll('a'):
    print (post.get('href'))

Tags: infromimportidurl标题pandasfor
1条回答
网友
1楼 · 发布于 2024-04-25 07:40:13

使用css选择器,速度更快。你知道吗

import requests
from bs4 import  BeautifulSoup

url = 'https://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw=xbox&_pgn=2&_skc=50&rt=nc'
Res = requests.get(url)
soup = BeautifulSoup(Res.text,'html.parser')
for post in soup.select("#ListViewInner a"):
    print(post.get('href'))

使用format()函数而不是串联字符串。你知道吗

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import urllib.request
from bs4 import BeautifulSoup

BaseURL = "https://www.ebay.co.uk/sch/i.html?_from=R40&_sacat=0&_nkw={}&_pgn={}&_skc={}&rt={}"

skc = "50"
rt = "nc"
Request = "xbox"
PageNumber = "2"

URL = BaseURL.format(Request,PageNumber,skc,rt)
print(URL)
HTML = urllib.request.urlopen(URL).read()
soup = BeautifulSoup(HTML,"html.parser")
for post in soup.select('#ListViewInner a'):
    print(post.get('href'))

相关问题 更多 >

    热门问题