我試圖抓取的網站是http://www.boxofficemojo.com/yearly/chart/?yr=2015&p=.htm。 這個網站有一個電影列表,並且對於每部電影,我想在表格中獲得以下信息,不包括日期。Python BeautifulSoup webcrawling:獲取沒有鏈接或類標籤的文本
我有這個麻煩,因爲文本沒有鏈接或任何類別的標籤。我嘗試過使用多種方法,但都沒有工作。
這是我迄今爲止的一種方法,只是爲了獲得每部電影的排名。 我所要的輸出只是每部電影的等級,然後有每個電影,週末總值的名單另一份名單的組成名單的列表等
listOfRanks = [[1, 1, 1,], [1, 2, 3], [3, 5,1]], etc.
listOfWeekendGross = [[208,806,270,106588440,54200000], [111111111, 222222222, 333333333]]
def getRank(item_url):
href = item_url[:37]+"page=weekend&" + item_url[37:]
response = requests.get(href)
soup = BeautifulSoup(response.content, "lxml") # or BeautifulSoup(response.content, "html5lib")
rank = soup.select('tbody > tr > td > center > table > tbody > tr > td > font')
print rank
這是我調用該函數 -
def spider(max_pages):
url = 'http://www.boxofficemojo.com/yearly/chart/?page=' + str(max_pages) + '&view=releasedate&view2=domestic&yr=2015&p=.htm'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.select('td > b > font > a[href^=/movies/?]'):
href = 'http://www.boxofficemojo.com' + link.get('href')
getRank(href)
問題是getRank(href)方法沒有正確地向列表添加行列。問題是這條線我認爲 -
rank = soup.select('tbody > tr > td > center > table > tbody > tr > td > font')
這可能不是正確的方式來獲取此文本。
如何從本網站獲得所有等級,週末總數等?
+++++++++++++++++++++++++++++++++
出於某種原因「child.text」行不起作用。我也試過child.string和child.getText()。具體的錯誤是UnicodeEncodeError:'charmap'編解碼器不能在位置6編碼字符u'\ x96':字符映射到。如果我只是打印標題部分,它的工作原理 –
alphamonkey
你確定嗎?這是我正在使用的整個腳本,它在我的機器上工作:https://ideone.com/Jt3OCh –
我認爲可能有錯誤的編碼文件,基於錯誤文件「C:/ Users/younjin /PycharmProjects/untitled/movies.py「,第96行,在getRank中 打印標題,」:「,child.text 文件」C:\ Python27 \ lib \ encodings \ cp1252.py「,第12行,編碼爲 return codecs.charmap_encode(input,errors,encoding_table) UnicodeEncodeError:'charmap'編解碼器無法編碼字符u'\ x96'在位置6:字符映射到 –
alphamonkey