【发布时间】:2015-02-24 16:23:19
【问题描述】:
我有一系列类似于“2014 年 12 月 27 日星期六”的字符串,我想扔掉“星期六”并保存名称为“141227”的文件,即年 + 月 + 日。到目前为止,一切正常,除了我无法让 daypos 或 yearpos 的正则表达式工作。他们都给出了同样的错误:
Traceback(最近一次调用最后一次):文件“scrapewaybackblog.py”,行 17、在 daypos = byline.find(re.compile("[A-Z][a-z]*\s")) TypeError: expected a character buffer object
什么是字符缓冲区对象?这是否意味着我的表达有问题?这是我的脚本:
for i in xrange(3, 1, -1):
page = urllib2.urlopen("http://web.archive.org/web/20090204221349/http://www.americansforprosperity.org/nationalblog?page={}".format(i))
soup = BeautifulSoup(page.read())
snippet = soup.find_all('div', attrs={'class': 'blog-box'})
for div in snippet:
byline = div.find('div', attrs={'class': 'date'}).text.encode('utf-8')
text = div.find('div', attrs={'class': 'right-box'}).text.encode('utf-8')
monthpos = byline.find(",")
daypos = byline.find(re.compile("[A-Z][a-z]*\s"))
yearpos = byline.find(re.compile("[A-Z][a-z]*\D\d*\w*\s"))
endpos = monthpos + len(byline)
month = byline[monthpos+1:daypos]
day = byline[daypos+0:yearpos]
year = byline[yearpos+2:endpos]
output_files_pathname = 'Data/' # path where output will go
new_filename = year + month + day + ".txt"
outfile = open(output_files_pathname + new_filename,'w')
outfile.write(date)
outfile.write("\n")
outfile.write(text)
outfile.close()
print "finished another url from page {}".format(i)
我还没有弄清楚如何使 12 月 = 12,但那是另一次了。请帮我找到合适的职位。
【问题讨论】:
标签: python html regex beautifulsoup html-parsing