python:在__getattr__中引发AttributeError,attr AttributeError:__len__

时间:2017-04-24 06:44:36

标签: python-2.7

List<String> reload;

private void inputBox_KeyDown(object sender, KeyEventArgs e){
        int x = 0;
        reload.item(x);//this is my bogus guess on how it should be done
            try{
                if (e.KeyCode == Keys.Enter){
                    itemHandler();
                    inputBox.Clear();
                }

                if (e.KeyCode == Keys.Up){
                    inputBox.Text = reload().item(x+1); //again bogus
                    x++;
                }

                if (e.KeyCode == Keys.Down)(
                    inputBox.Text = reload().item(x-1); //again bogus
                    x--;
                }

                else {}
            }

            catch(Exception ex)
            {
                rtbDisplay.AppendText("Error:" + ex );
            }
        }

这是我正在使用的代码。提到的是从另一个函数请求的URL。

def get_site(r):
    from bs4 import BeautifulSoup
    soup=BeautifulSoup(r, 'lxml')

运行python代码时显示的错误是:

r=urllib2.request(url)

你能帮我解决一下吗?网址是https格式,我使用的是python 2.7

更新:整个代码在这里:

File "/usr/lib/python2.7/urllib2.py", line 229, in __getattr__ raise AttributeError, attr AttributeError: __len__ `

显式地在终端上调用url,并在下面的注释中提到url

1 个答案:

答案 0 :(得分:0)

尝试使用此代替请求

from bs4 import BeautifulSoup
import urllib2
url='https://github.com/johnpapa?tab=followers'
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content, 'html.parser')
for lt in soup.find_all("a",class_="url"):
    if lt.get("href"):
        si=lt.get("href")
        print si
  

xxx@xxxx-xxxx:~/Desktop$ python sa.py http://johnpapa.net

这里是你如何转到关注页面并抓住他的粉丝 来自bs4 import BeautifulSoup

import urllib2
url='https://github.com/johnpapa?tab=followers'
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content, 'html.parser')
for lt in soup.find_all("a",class_="url"):
    if lt.get("href"):
        si=lt.get("href")
        print si
for item in soup.find_all("a",class_="d-inline-block no-underline mb-1"):
        url1=item.get("href")
        url1='https://github.com'+url1+'?tab=followers'
        print url1
        content = urllib2.urlopen(url1).read()
        soup1 = BeautifulSoup(content, 'html.parser')
        for lt in soup1.find_all("a",class_="url"):
            if lt.get("href"):
                si=lt.get("href")
                print si
        for item in soup1.find_all("a",class_="d-inline-block no-underline mb-1"):
                url1=item.get("href")
                url1='https://github.com'+url1+'?tab=followers'
                print url1

输出:

  

xxxx@xxxx-xxxx:~/Desktop$ python sa.py http://johnpapa.net https://github.com/gu1ma?tab=followers ...