将字符串转换为列表

时间:2016-07-09 10:20:12

标签: python python-2.x

我认为我试图变成一个列表是一个字符串。

所以当我运行它时会打印出一个网址列表。我想把这些网址变成这样的列表:

["Apple", "Pear", "Radio"]

代码:

url = "http://www.wired.com/category/science/page/"
a = list(range(1, 12))
i=0


while i < len(a):
    urls1 = url + str(i)
    print urls1[1]
    i+=1

我想要做的事情:(我想如果我将两者结合起来,我就可以使用刮刀来获取网址。)

import urllib
import re


urls = [urls1]
#https://www.wired.com/2016/07/google-tests-new-crypto-chrome-fend-off-quantum-attacks/
i=0

regex = '<h2 class="title brandon clamp-5">(.+?)</h2>'

#https?:(\/\/www\.wired\.com\/2016(\/[A-Za-z0-9\-\._~:\/\?#\[\]@!$&'\(\)\*\+,;\=]*)?)

pattern = re.compile(regex)

while i < len(urls):
    htmlfile = urllib.urlopen(urls[i])
    htmltext = htmlfile.read()
    titles = re.findall(pattern, htmltext)



    for elem in titles: 
        print elem 

    i+=1

1 个答案:

答案 0 :(得分:0)

获取11个网址列表:

url = "http://www.wired.com/category/science/page/"
urls = []
for i in range(1, 12):
    new_url =  url + str(i)
    urls.append(new_url)
print urls