web scraper脚本错误问题

时间:2017-07-18 00:02:06

标签: android python

我刚开始使用python进行编码只做了两个月和一点点。我用qpython在我的Android设备上编码。我为了好玩而编码,我将在以后获利。

我研究了一本电子书,里面有一些网络刮刀代码。我目前正在摆弄此代码的副本并遇到此错误:Traceback(最近一次调用最后一次):

File "/storage/emulated/0/qpython/scripts/.last_tmp.py", line 25,
in <module> Scraper(news).scrape() 
File"/storage/emulated/0/qpython/scripts/.last_tmp.py", line 14, 
in _init_ html = r.read() NameError: global name 'r' is not defined. 1|u0_a162@g3:/ $ 

我认为它可能就像在scrape()中使用局部变量一样简单但不确定。这是代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup 
class Scraper: 
    def __init__( self, site): 
        self.site = site 


        def scrape( self): r = urllib.request\
         .urlopen("http://news.google.com/") 
        html = r.read() 
        parser = "html.parser" 
        sp = BeautifulSoup( html, parser) 
        for tag in sp.find_all("a"): 
            url = tag.get("href") 
            if url is None: continue 
            if "html" in url: 
                print("\n" + url) 


news = " https:// news.google.com/" 
Scraper(news).scrape() 

我只是想知道我需要做什么才能使这个脚本运行起来以及为什么我需要做这些事情。我的笔记本电脑上有python 3,并从那里运行这个脚本以及我的Android手机。

0 个答案:

没有答案