【发布时间】:2017-04-27 20:41:50
【问题描述】:
帮助,我正在从一个网站获取多个 href url 链接,并尝试将 url 的每个标题和正文附加到另一个数组中。但是,当我运行类似的东西时,我只抓住一个标题,将其他链接的所有文本放在一起。
request = requests.get(url)
somecontents = request.content
soup = BeautifulSoup(somecontents, "html.parser")
soup.prettify()
gethref = urllinks.get("href")
if gethref is not None and\
"http" in gethref and\
"photo" not in gethref and\
"img" not in gethref:
page_links = []
tags_in_link = gethref
page_links.append(tags_in_link)
hrefdataset = ','.join(page_links)
for each_link in i:
website_header_title = soup.title.string
parse_title = re.sub('[^A-Za-z]+', ' ', website_header_title)
time.sleep(.05)
done = grab_web_text(each_link)
testintry = []
testintry.append("Website Title: " + parse_title + "," + " ")
text = testintry.append("Body: " + done)
我想要每个链接:我怎样才能将其格式化为我所拥有的?
[{"Website Title: " "title", "Body: " "Body},
[{"Website Title: " "title", "Body: " "Body},
[{"Website Title: " "title", "Body: " "Body},
[{"Website Title: " "title", "Body: " "Body}]
【问题讨论】:
标签: python web-scraping beautifulsoup