python - python中的beautifulsoup解析错误——垃圾字符

标签 python beautifulsoup mechanize

代码 - 不确定我做了什么让 BeautifulSoup (BS) 不起作用

import mechanize
import urllib2
from bs4 import BeautifulSoup

#create a browser object to login
browser = mechanize.Browser()

#tell the browser we are human, and not a robot, so the mechanize library doesn't block us
browser.set_handle_robots(False)

browser.addheaders = [('User-Agent','Mozilla/5.0 (Windows U; Windows NT 6.0; en-US; rv:9.0.6')]
#url
url = 'https://www.google.com.au/search?q=python'
#open the url in our virtual browser
browser.open(url)
html = browser.response().read()
print html
soup = BeautifulSoup(html)
print(soup.prettify())

错误
HTMLParseError: junk characters in start tag: u'{t:1}); class="gbzt ', at line 1, column 42892

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-AU"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/google_favicon_128.png" itemprop="image"><title>python - Google Search</title><style>#gb{font:13px/27px Arial,sans-serif;height:30px}#gbz,#gbg{position:absolute;white-space:nowrap;top:0;height:30px;z-index:1000}#gbz{left:0;padding-left:4px}#gbg{right:0;padding-right:5px}#gbs{background:transparent;position:absolute;top:-999px;v

最佳答案

尝试使用 requests :

import requests
from bs4 import BeautifulSoup
#url
url = 'https://www.google.com.au/search?q=python'
r=requests.get(url)
html = r.text
print html
soup = BeautifulSoup(html)
print(soup.prettify())

关于python - python中的beautifulsoup解析错误——垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23727316/

相关文章:

python - 处理包含子列表的父列表(带有变量计数)

python - 用Python抓取具有多个输入的网页

ruby - 在 Mechanize 中使用登录表单

python - 代理似乎被 Mechanize 忽略了?

python - matplotlib 中的两点线段图

python - 在字符串的子字符串之前插入字符串

python - Keras:如何保存模型并继续训练?

python-2.7 - 使用 BeautifulSoup 从 div 中的所有 p 元素中获取文本

python - 使用 BeautifulSoup,如何仅从特定选择器中获取文本而没有子项中的文本?

python - 使用 Python 请求选择表单