【发布时间】:2016-04-28 10:55:34
【问题描述】:
BeautifulSoup 上有很多内容,但我找不到任何答案...我想通过在代码中指定前后的文本位来提取 html 的两位之间的文本。我可以用 Outwit Python 模块做到这一点,但这次需要使用 BeautifulSoup...
我想要的页面是下面的用户名:
<a class="generic_class" href="/people/username">
所以,我想指定beautifulsoup 通过告诉它查找来抓取用户名
'a class="generic_class" href="/people/'
前位刮后停
'"'
然后我希望它在来自 csv 的 url 循环中执行此操作(这已经有效),然后将结果逐行附加到新的 csv(此位可能不起作用):
for row in url_reader:
url = row[0]
page = br.open(url).read()
soup = BeautifulSoup(br.response().read())
user = soup.findAll('<a class="generic_class" href="/people/') # this is the line where the code that works should go! Obviously this bit does nothing as it doesn't extract what comes after, stopping at the closing quotation mark for the end of the href.
page.append.user(output_file) # not sure if this is right?!
显然,在理想情况下,我会将其放在 if/else to if(查找“未找到页面”)和 else(执行上述操作)中以处理不起作用的 url,但我一旦我可以真正使事情正常工作,就会进行错误处理!这是我现在的首要任务...
非常感谢任何帮助。
【问题讨论】:
标签: python python-2.7 csv web-scraping beautifulsoup