好吧,決定編輯這個真正的快速,只是在這裏包括我的整個劇本。如何從html中將特定位的信息提取到python webscraper?
這是我第一個使用python的「真實世界」應用程序,或者對於這個問題,任何編程語言。 我已經使用我發現的教程構建了一個基本的網頁抓取工具,並且正在嘗試添加信息並在其基礎上創建一些比實際更有用的工具。 我遇到的問題是,收集的信息到目前爲止一直比較容易獲得,但現在我被困在這裏將代碼的代價拉到我的python腳本中。
我可以從這裏得到價格,但是它帶有字符和空格的可怕格式,我似乎無法弄清楚如何去掉,這完全毀了我的.csv文件在代碼的末尾。
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
my_url = "https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?
Tpk=graphics%20cards"
#Grabbing the page
uClient = uReq(my_url)
#Reading uCLient and saving contents as a variable called page_html (it is
raw html code)
page_html = uClient.read()
#Close the web connection to uCLient
uClient.close()
#html parsing
page_soup = soup(page_html, "html.parser")
#Grabs graphics card containers/each product
containers = page_soup.findAll("div",{"class":"item-container"})
filename = "products.csv"
f = open(filename, "w")
headers = "Brand, Product Name, Price, Shipping\n"
f.write(headers)
for container in containers:
brand = container.div.div.a.img["title"]
title_container = container.findAll("a",{"class":"item-title"})
product_name = title_container[0].text
price_container = container.findAll("li",{"class":"price-current"})
price = price_container[0].text.strip('|')
shipping_container = container.findAll("li",{"class":"price-ship"})
shipping = shipping_container[0].text.strip()
print("Brand: " + brand)
print("Product name: " + product_name)
print("Price: " + price)
print("Shipping: " + shipping)
f.write(brand + "," + product_name.replace(",","|") + "," + price + ","
+ shipping + "\n")
f.close()
的HTML數據時運行shipping_container看起來是這樣的:
<li class="price-current">
<span class="price-current-label">
<a class="membership-info membership-popup" data-neg-popid="MembershipPopup"
href="javascript:void(0);" name="membership" style="display: inline"><span
class="membership-icon"></span><span style="display: none">|</span></a>
</span>$<strong>249</strong><sup>.99</sup> <a class="price-current-num"
href="https://www.newegg.com/Product/Product.aspx?
Item=N82E16814150795&buyingoptions=New&ignorebbr=1">(10 Offers)</a>
<span class="price-current-range">
<abbr title="to">–</abbr>
</span>
</li>
正如你可能已經想通了,我要的是打印(價格)249.99只顯示。我在這裏搞砸了什麼,或者失蹤了?
這是被導入到我的實際.csv文件 '| \ n $ 249.99 \ XA0(10個報價)\ n \正'
或
| $ 249.99(19個報價)
-
這裏是我的工作的link。
老兄,說實話,我不是在尋找一個「爲我而做」的回答,而是我可以學習的東西。我只是想弄清楚爲什麼它是我得到這個混亂的爛攤子,爲什麼
>>> price = price_container[0].findAll('li',{'/span':'strong'})
>>> price
OR
>>> price = price_container[0].findAll('li',{'strong':'strong'})
>>> price
回報什麼,但沒有錯誤...
哪個庫您使用? –
BeautifulSoup4。 – user8628012