我想用类列表一章从UL获得所有链接,但是我只得到了我想要的链接的一半,因为链接在两个<ul>
中分开,这两个<div><ul>links1</ul><ul>links2</ul></div>
位于一个类似so <div><ul>links1</ul><ul>links2</ul></div>
的div中。我是python的新手,我真的被困住了。你知道吗
如果可能的话,我还想在everylink之前添加“http://www.example.com”,并将它们逐个保存在一个列表中,这样我就可以使用list[1]访问它们。你知道吗
谢谢,这是密码
# import libraries
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
"""Getting Started Example for Python 2.7+/3.3+"""
chapter = 1
chapterlist = 1
links = []
name = ""
reallink = ""
while chapter < 31:
quote_page = Request('http://website.com/page.html?page=' + str(chapter) + '&per-page=50', headers={'User-Agent': 'Mosezilla/5.0'})
page = urlopen(quote_page).read()
soup = BeautifulSoup(page, "html.parser")
name_box = soup.find("ul", attrs={"class": "list-chapter"})
links += name_box.find_all("a")
reallink += str([a['href'] for a in links])
chapter += 1
f = open("links.txt", "w+")
i = 1
f.write(reallink)
f.close()
您使用的
find
将返回第一个匹配项,而find_all
将返回匹配项列表。你知道吗假设您的
ul
类是正确的,我将使用select
代替,并收集这些类的子a
标记:替换这些行:
与
相关问题 更多 >
编程相关推荐