搜索和谷歌上搜索了一陣後,我遇到了一個叫GSMArena網站。現在,該網站是用於手機和平板電腦的比較網站,您基本上可以看到平板電腦/手機的所有規格。查看搜索頁面的源代碼,我發現有一個包含所有搜索結果的類「maker」的div。
此外,一旦您點擊手機/平板電腦鏈接,它會將您帶到標題爲「平板電腦名稱 - 完整平板電腦規格」的頁面(如果其選項卡),以及「移動設備名稱 - 完整手機規格」(如果它是移動設備)。
如果發現與搜索查詢直接匹配,它會直接重定向到規範頁面,所以我添加了一個If測試以檢查它是否爲搜索頁面或規範頁面。
我的程序獲取「製造商」div中的第一個鏈接(使用BeautifulSoup),然後進入鏈接,拉出html,然後獲取頁面的標題。
如果沒有找到結果,我的程序將其標記爲 「其他」
代碼:
import urllib.request
from bs4 import BeautifulSoup
searchlist = ["galaxy note","nexus 10","nexus 5","galaxy ace","moto g","galaxy tab 2", "MID-97D"]
for searchstr in searchlist:
other = False
searchstr = searchstr.replace(" ", "%20")
searchlink = "http://www.gsmarena.com/results.php3?sQuickSearch=yes&sName="+searchstr
string = urllib.request.urlopen(searchlink).read().decode("ISO-8859-1")
soup = BeautifulSoup(string,"lxml")
if soup.title.string == "Phone Finder results - GSMArena.com":
makerdiv = soup.find_all('div', attrs={'class': 'makers'})
links = makerdiv[0].find_all('a')
if len(links) != 0:
link = "http://www.gsmarena.com/" + links[0].attrs['href']
string = urllib.request.urlopen(link).read().decode("ISO-8859-1")
soup = BeautifulSoup(string,"lxml")
else:
other = True
if other == False:
title = soup.title.string
name = title.split("-")[0]
rest = title.split("-")[1]
taborphone = rest.split(" ")[2]
else:
name = searchstr
taborphone = "other"
print("Name:",name)
print("Type:",taborphone)
輸出:
Name: Samsung Galaxy Note5
Type: phone
Name: Samsung Google Nexus 10 P8110
Type: tablet
Name: LG Nexus 5X
Type: phone
Name: Samsung Galaxy Ace 3
Type: phone
Name: Motorola Moto G (3rd gen)
Type: phone
Name: Samsung Galaxy Tab 2 7.0 P3100
Type: tablet
Name: MID-97D
Type: other
而且它的工作原理:)
優點:
數據庫將始終保持更新,並GSMArena有很多手機和平板電腦在其數據庫
缺點:
它不能被用於除平板電腦和手機等設備,如上網本等
我只是注意到@Oli在評論中建議GSMArena。
GSMArena可能有幫助嗎? http://www.codeproject.com/Articles/660151/Screen-scraping-using-YQL-and-AJAX – Oli