如何从html中将特定信息提取到python webscraper?

时间:2017-10-19 16:13:57

标签: python html web-scraping

好的,决定快速编辑这个,然后在这里包含我的整个脚本。

这是我的第一个现实世界'使用python的应用程序,或者就此而言,任何编程语言。 我使用我发现的教程构建了一个基本的Web scraper,我正在尝试添加信息并在其上构建,以创建比它更有用的东西。 我遇到的问题是所收集的信息到目前为止相对容易实现,但现在我不得不把这些代码的价格从这里拉到我的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()

运行shipping_container时的html位如下所示:

<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&amp;buyingoptions=New&amp;ignorebbr=1">(10 Offers)</a>
   <span class="price-current-range">
   <abbr title="to">–</abbr>
   </span>
</li>

你可能已经想到了,我想要的是打印(价格)只显示249.99。我在这里搞砸了什么,或者失踪了?

这是导入到我的实际.csv文件的内容&#39; | \ n $ 249.99 \ xa0(10个优惠)\ n \ n - &#39;

| $ 249.99(19报价)

-

link正在与我合作。

老实说,老实说,我不是在寻找一个&#34;为我做这件事&#34;答案,但我可以从中学到一些东西。我只想弄清楚为什么我会弄得这乱糟糟的混乱以及为什么

>>> price = price_container[0].findAll('li',{'/span':'strong'})
>>> price

OR

>>> price = price_container[0].findAll('li',{'strong':'strong'})
>>> price

什么都不返回,但没有错误...

2 个答案:

答案 0 :(得分:2)

以下是从您提供的示例HTML中抓取数据的代码:

# We are using BeautifulSoup library for scraping.
from bs4 import BeautifulSoup

if __name__ == "__main__":

    temp = 'REPLACE THIS STRING WITH THE ABOVE SAMPLE HTML'

    # For using this in the real website, you can iterate over the lists with class 'price-current'
    soup = BeautifulSoup(temp, 'html.parser')
    dollars = soup.find("strong").text
    cents = soup.find("sup").text
    print(dollars + cents)

以下是一些很酷的库,您可以在您的抓取项目中使用它们:

  1. BeautifulSoup
  2. Scrapy
  3. Requests
  4. 注意:您应该始终检查从该网站获取数据是否合法。

答案 1 :(得分:0)

给它一个机会。它会以更清洁的方式为您提供价格。顺便说一句,请考虑下面刮刀中的html_elem是您粘贴在上面的html elements的替代名称。

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_elem,"lxml")
print(''.join([item.text.strip() for item in soup.select("strong,sup") if item.text.split()]))

结果:

249.99