使用Python进行数据抓取

使用Python进行数据抓取,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我使用Python开发了一个脚本,从这个URL中提取手机名称 这是我的剧本: from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup my_url = 'https://www.jumia.com.ng/mobile-phones/' uClient =uReq(my_url) #open connection.. grab the page page_html = uClient.rea

我使用Python开发了一个脚本,从这个URL中提取手机名称

这是我的剧本:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.jumia.com.ng/mobile-phones/' 
uClient =uReq(my_url) #open connection.. grab the page
page_html = uClient.read() #load the content into a varaible
uClient.close()  #close the console
page_soup = soup(page_html, "html.parser") #it does the html parser
phone_name = page_soup.findAll("span",{"class":"name"}) #grabs each phone name
print (phone_name)
我的预期结果应该是这样的:

Marathon M5 Mini 5.0-Inch IPS (2GB, 16GB ROM) Android 5.1 Lollipop, 13MP + 8MP Smartphone - Grey
但我得到的是:

<span class="name" dir="ltr">Marathon M5 Mini 5.0-Inch IPS (2GB, 16GB ROM) Android 5.1 Lollipop, 13MP + 8MP Smartphone - Grey</span>.
marathonm5迷你5.0英寸IPS(2GB,16GB ROM)安卓5.1棒棒糖,13MP+8MP智能手机-灰色。

如何从这款
马拉松M5迷你5.0英寸IPS(2GB,16GB ROM)安卓5.1棒棒糖,13MP+8MP智能手机中提取文本-灰色

要提取名称,请使用
。text

>>> for phone_name in page_soup.findAll("span",{"class":"name"}):
        print(phone_name.text)

Boom J8 5.5 Inch (2GB, 16GB ROM) Android Lollipop 5.1 13MP + 5MP Smartphone - White (MWFS)
Marathon M5 Mini 5.0-Inch IPS (2GB, 16GB ROM) Android 5.1 Lollipop, 13MP + 8MP Smartphone - Grey
因此,您的脚本应该如下所示:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.jumia.com.ng/mobile-phones/' 
uClient =uReq(my_url) #open connection.. grab the page
page_html = uClient.read() #load the content into a varaible
uClient.close()  #close the console
page_soup = soup(page_html, "html.parser") #it does the html parser
for phone_name in page_soup.findAll("span",{"class":"name"}):
    print(phone_name.text)

在你的问题中,你得到的和期望得到的是一样的。你读过吗?使用python请求。这比你所得到的和你的问题所期望的都好。。。我得到了这款马拉松M5迷你5.0英寸IPS(2GB,16GB ROM)安卓5.1棒棒糖,13MP+8MP智能手机-灰色。我的预期结果应该是这款马拉松M5迷你5.0英寸IPS(2GB,16GB ROM)安卓5.1棒棒糖,13MP+8MP智能手机-灰色。感谢您的回复。您阅读了BeautifulSoup文档了吗?不我现在就这么做。。。希望对你有帮助。。。谢谢你的回复。。