网站抓取脚本在 Linux 中有效,但在 Windows 中无效 7?
Website scraping script works in Linux but not in Windows 7?
我写了一个抓取 URL 的脚本。它在 Linux OS 上运行良好。但是当 运行ning on Windows 7 时我收到 http 503 错误。URL 有一些问题。
我正在使用 python 2.7.11 。
请帮忙。
以下是脚本:
import sys # Used to add the BeautifulSoup folder the import path
import urllib2 # Used to read the html document
if __name__ == "__main__":
### Import Beautiful Soup
### Here, I have the BeautifulSoup folder in the level of this Python script
### So I need to tell Python where to look.
sys.path.append("./BeautifulSoup")
from bs4 import BeautifulSoup
### Create opener with Google-friendly user agent
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
### Open page & generate soup
### the "start" variable will be used to iterate through 10 pages.
for start in range(0,1000):
url = "http://www.google.com/search?q=site:theknot.com/us/&start=" + str(start*10)
page = opener.open(url)
soup = BeautifulSoup(page)
### Parse and find
### Looks like google contains URLs in <cite> tags.
### So for each cite tag on each page (10), print its contents (url)
file = open("parseddata.txt", "wb")
for cite in soup.findAll('cite'):
print cite.text
file.write(cite.text+"\n")
# file.flush()
# file.close()
如果您在 windows 7 中 运行,cmd 将抛出 http503 错误,说明问题出在 url。
URL 在 Linux OS 中工作正常。如果 URL 实际上是错误的,请提出替代方案。
显然 Python 2.7.2 在 Windows 上,任何时候您发送自定义 User-agent header,urllib2 都不会发送那个 header . (来源:https://whosebug.com/a/8994498/6479294)。
因此您可能要考虑在 Windows 中使用 requests 而不是 urllib2:
import requests
# ...
page = requests.get(url)
soup = BeautifulSoup(page.text)
# etc...
编辑:另外一个非常好的观点是 Google 可能会阻止您的 IP - 他们真的不喜欢机器人连续发出 100 个奇怪的请求。
我写了一个抓取 URL 的脚本。它在 Linux OS 上运行良好。但是当 运行ning on Windows 7 时我收到 http 503 错误。URL 有一些问题。 我正在使用 python 2.7.11 。 请帮忙。 以下是脚本:
import sys # Used to add the BeautifulSoup folder the import path
import urllib2 # Used to read the html document
if __name__ == "__main__":
### Import Beautiful Soup
### Here, I have the BeautifulSoup folder in the level of this Python script
### So I need to tell Python where to look.
sys.path.append("./BeautifulSoup")
from bs4 import BeautifulSoup
### Create opener with Google-friendly user agent
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
### Open page & generate soup
### the "start" variable will be used to iterate through 10 pages.
for start in range(0,1000):
url = "http://www.google.com/search?q=site:theknot.com/us/&start=" + str(start*10)
page = opener.open(url)
soup = BeautifulSoup(page)
### Parse and find
### Looks like google contains URLs in <cite> tags.
### So for each cite tag on each page (10), print its contents (url)
file = open("parseddata.txt", "wb")
for cite in soup.findAll('cite'):
print cite.text
file.write(cite.text+"\n")
# file.flush()
# file.close()
如果您在 windows 7 中 运行,cmd 将抛出 http503 错误,说明问题出在 url。 URL 在 Linux OS 中工作正常。如果 URL 实际上是错误的,请提出替代方案。
显然 Python 2.7.2 在 Windows 上,任何时候您发送自定义 User-agent header,urllib2 都不会发送那个 header . (来源:https://whosebug.com/a/8994498/6479294)。
因此您可能要考虑在 Windows 中使用 requests 而不是 urllib2:
import requests
# ...
page = requests.get(url)
soup = BeautifulSoup(page.text)
# etc...
编辑:另外一个非常好的观点是 Google 可能会阻止您的 IP - 他们真的不喜欢机器人连续发出 100 个奇怪的请求。