Fetch a Wikipedia article with Python

前端 未结 10 1890
余生分开走
余生分开走 2020-11-27 15:37

I try to fetch a Wikipedia article with Python\'s urllib:

f = urllib.urlopen(\"http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes\")         


        
10条回答
  •  温柔的废话
    2020-11-27 16:11

    You need to use the urllib2 that superseedes urllib in the python std library in order to change the user agent.

    Straight from the examples

    import urllib2
    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    infile = opener.open('http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes')
    page = infile.read()
    

提交回复
热议问题