python中的BeautifulSoup或urllib.request在不同的机器上返回不同的
所以我写了这个简单的脚本,但它只在我的linux机器上工作,而不是在Windows8.1上 代码是:python中的BeautifulSoup或urllib.request在不同的机器上返回不同的,python,beautifulsoup,urllib,Python,Beautifulsoup,Urllib,所以我写了这个简单的脚本,但它只在我的linux机器上工作,而不是在Windows8.1上 代码是: BASE_URL = "http://www.betfair.com/exchange/football/event?id="+ str(matchId) html = urlopen(BASE_URL).read() soup = BeautifulSoup(html) homeScore = soup.find_all("span", {"class": "home-score"})[0].
BASE_URL = "http://www.betfair.com/exchange/football/event?id="+ str(matchId)
html = urlopen(BASE_URL).read()
soup = BeautifulSoup(html)
homeScore = soup.find_all("span", {"class": "home-score"})[0].text
在我的Windows 8计算机上,它从urlopen返回:
html bytes: b'\\n\\n \\n <!DOCTYPE html>\\n\\n <!--[if IE]><![endif]-->\\n\\n <!--[if IE 9]>\\n <html class="ie9" lang="da-DK"><![endif]--><!--[if IE 8]>\\n <html class="ie8" lang="da-DK"><![endif]--><!--[if IE 7]>\\n <html class="ie7" lang="da-DK"><![endif]--><!--[if lt IE 7]>\\n <html class="ie6" lang="da-DK"><![endif]-->\\n <!--[if (gt IE 9)|!(IE)]><!-->\\n <html lang="da-DK">\\n <!--<![endif]-->\\n <head>\\n <meta name="description" content="San Luis de Quillota v Deportes Temuco ma 15 dec 2014 11:00PM - betting odds. Find markedets bedste spil, samt links til andre ressourcer.">\\n <meta charset="utf-8">\\n <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>\\n <base href="http://www.betfair.com/exchange/"/>\\n <title> San Luis de Quillota v Deportes Temuco betting odds | Chilean Primera B | betfair.com\\n</title>\\n\\n <link rel="shortcut icon" href="//sn4.cdnbf.net/exchange/favicon_13031_....
点是输出的实际结束。如何使相同的代码在两个系统上都工作
< P>编辑:我的Windows 8是Python 3.4,Linux是Python 3.2 < P>如果你有机会,你应该考虑使用UrLIB。
如果构建的请求能够无缝地处理多种格式,那么它应该可以在您的每个平台上工作。如果不是这样,请测试r.content.decode的不同参数,但无论如何,这比使用urllib要容易得多。在linux上,您从代码中得到了什么?Win8.1上的html数据与linux上的html数据有什么区别?我的linux机器获取站点的完整html,并且它能够使用正确的类找到预期的范围
import requests
from bs4 import BeautifulSoup
base_url = 'http://www.betfair.com/exchange/football/event'
params = { 'id': str(matchId) }
r = requests.get(base_url, params=params)
html = r.content.decode('utf-8', 'ignore')
soup = BeautifulSoup(html, "lxml")