【发布时间】:2020-06-30 15:00:39
【问题描述】:
我正在尝试使用创建链接的方法来抓取网页,但是当链接中包含阿塞拜疆字母时会出现问题。 Python 拆分链接从阿塞拜疆字母表开始。
一个例子:
链接应该像 => http://marja.az/search?q=ali+əli
但它会像 => 一样打印它 http://marja.az/search?q=ali+əli
代码:
import requests
from bs4 import BeautifulSoup as bs
import locale
URL = 'http://marja.az/search?q='
# if there is a prabel inside of keyword merge with + sign
KEYWORDS = [
'Vali+Vəli'
]
for key in KEYWORDS:
search_url = URL + key
print(search_url)
r = requests.get(search_url)
soup = bs(r.content, "lxml")
for data in soup.find_all("div", {"class": "searchNews"}):
for a in data.find_all("a"):
href = a.get("href")
link = "http://marja.az/" + href
print(link)
r1 = requests.get(link)
soup1 = bs(r1.content, "lxml")
# HEADER of NEWS
header = soup1.find("h1", attrs={"class": "title"}).text
print(header)
# CONTENT of NEWS
paragraph = soup1.find("div", attrs={"class": "text"}).findAll('p', text=True, recursive=False)
for p in paragraph:
print(p.text)
# DATE of NEWS PUBLISHED
date = soup1.find('div', {'style': 'color: #af0000; margin:10px 0px 10px 0px; font-size:12px; ''font'
'-weight:bold; text-align:left;'}).text
date = date.split(",")[0].split(" ")
date = date[0] + "-" + date[1] + "-" + date[2]
print(date)
【问题讨论】:
标签: python-3.x unicode beautifulsoup