A usage in python is unclear
阿神
阿神 2017-05-18 10:59:46
0
1
824

In the process of writing a crawler in Python (crawling Wikipedia entries), the url["href"] appeared during the output of the iterator. I thought it should be a usage in the iterator, but I couldn't find it. Please help. This usage means, thank you

#coding:utf-8

import urllib
import urllib2
import re
from bs4 import BeautifulSoup

resp = urllib2.urlopen("https://en.wikipedia.org/wiki/Main_Page").read()

soup = BeautifulSoup(resp,"html.parser")

listurl = soup.findAll('a',href=re.compile("^/wiki/"))
for url in listurl:
        print url.get_text(),"------>","https://en.wikipedia.org"+url["href"]

The url["href"] in the last line has a truncation effect on the crawled data. Before adding it, the output is:
print url
Output: Disclaimers
After adding it, the output is: :
print url["href"]
Output:/wiki/Wikipedia:General_disclaimer
Solution, thank you

阿神
阿神

闭关修行中......

reply all(1)
某草草

As long as the class implements the __getitem__ method, you can use square brackets to get the value.

                                                   
In [16]: class A():                                
    ...:     def __getitem__(self,a):              
    ...:         return a                          
    ...:                                           
                                                   
In [17]: a = A()                                   
                                                   
In [18]: a['a'], a[1]                              
Out[18]: ('a', 1)                                  
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template