把线变成线

2024-06-16 13:52:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想我想要变成一个列表的是一个字符串。你知道吗

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

["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

我想做的是:(我想如果我把两者结合起来,我就可以让url和scraper一起使用。)

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

Tags: httpsimportrecomurl列表lenwww