我试图抓取的网站是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