Python抓取:试图根据用户输入抓取特定数据(电话详细信息)



我正在www.gsmarena.com上进行web抓取。我想根据用户输入提取特定的数据。这个代码返回所有的手机型号和名称,我想提取的只是三星手机的细节与特定的输入吸吮RAM,ROM,CPU和颜色。请帮帮我。提前谢谢。

import requests
from bs4 import BeautifulSoup

def link_scan(link_url):
c = 1
source_code=requests.get(link_url)
plain_text=source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.find_all('div',{'class':'brandmenu-v2 light l-box clearfix'}):
for li in link.find_all('li'):
for anc in li.find_all('a'):
anc_src = r'http://www.gsmarena.com/' + anc.get('href')
anc_name = anc.string
print(c, anc_name,"n", anc_src, "n")
c += 1
inside_scan(anc_name, anc_src)

def inside_scan(name, hrefs):
i = 1
source_code=requests.get(hrefs)
plain_text=source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.find_all('div',{'class':'makers'}):
for li in link.find_all('li'):
for anc in li.find_all('a'):
for nam in (sp.find('span') for sp in anc.find_all('strong')):
modal_name = nam.string
print("t", i, "t", name, modal_name)
i += 1
link_scan(r'http://www.gsmarena.com/')

我建议你找个时间玩url。在您的情况下,用户可能会要求特定的手机制造商和目标网址如下:

https://www.gsmarena.com/samsung-phones-9.php

此外,你很幸运,因为你可以在不重定向到手机页面的情况下获取特定的手机详细信息。在你的情况下,每个手机都引用了一个类名的锚标签:

<a href="samsung_galaxy_m31s-10333.php">

这意味着您可以解析从"开始的链接;Samsung";以便根据用户的需求过滤查询:

https://www.gsmarena.com/samsung

要获取CPU、RAM、e.t.c信息,您必须参考锚点标签:

<a href="samsung_galaxy_m31s-10333.php"><img src="https://fdn2.gsmarena.com/vv/bigpic/samsung-galaxy-m31s.jpg" title="Samsung Galaxy M31s Android smartphone. Announced Jul 2020. Features 6.5″ Super AMOLED display, Exynos 9611 chipset, 6000 mAh battery, 128 GB storage, 8 GB RAM, Corning Gorilla Glass 3."><strong><span>Galaxy M31s</span></strong></a>

最新更新