我正在使用BeautifulSoup编写一个使用Python的爬虫,一切都在游泳,直到我遇到这个网站:
我正在获取请求库的内容:
r = requests.get('http://www.elnorte.ec/')
content = r.content
Run Code Online (Sandbox Code Playgroud)
如果我在那时打印内容变量,所有西班牙语特殊字符似乎都正常工作.但是,一旦我尝试将内容变量提供给BeautifulSoup,一切都搞砸了:
soup = BeautifulSoup(content)
print(soup)
...
<a class="blogCalendarToday" href="/component/blog_calendar/?year=2011&month=08&day=27&modid=203" title="1009 artÃculos en este dÃa">
...
Run Code Online (Sandbox Code Playgroud)
它显然是在拼乱所有西班牙语的特殊角色(口音和诸如此类的东西).我尝试过做content.decode('utf-8'),content.decode('latin-1'),也尝试将fromEncoding参数搞砸到BeautifulSoup,将其设置为fromEncoding ='utf-8'和fromEncoding ='拉丁-1',但仍然没有骰子.
任何指针都将非常感激.
我正在尝试加载一个html页面并输出文本,即使我正确地获取网页,BeautifulSoup会以某种方式破坏编码.
资源:
# -*- coding: utf-8 -*-
import requests
from BeautifulSoup import BeautifulSoup
url = "http://www.columbia.edu/~fdc/utf8/"
r = requests.get(url)
encodedText = r.text.encode("utf-8")
soup = BeautifulSoup(encodedText)
text = str(soup.findAll(text=True))
print text.decode("utf-8")
Run Code Online (Sandbox Code Playgroud)
摘录输出:
...Odenw\xc3\xa4lderisch...
Run Code Online (Sandbox Code Playgroud)
这应该是Odenwälderisch
我有一个简单的项目,从旅游网站抓取评论并将其存储在excel文件中.评论可以是西班牙语,日语或任何其他语言,评论有时也包含特殊符号,如"❤❤".
我需要存储所有数据(如果无法写入,可以排除特殊符号).
我能够抓取我想要的数据并将其打印在控制台中(如日文文本),但问题是将其存储在csv文件中,它显示错误消息,如下所示
我尝试使用utf-8编码打开文件(如下面的评论中所述),但随后它将数据保存在一些没有意义的奇怪符号中....并且无法找到问题的答案.有什么建议.
我使用的是python 3.5.3
我的python代码:
from selenium import webdriver
from bs4 import BeautifulSoup
import time
import re
file = "TajMahalSpanish.csv"
f = open(file, "w")
headers = "rating, title, review\n"
f.write(headers)
pages = 119
pageNumber = 2
option = webdriver.ChromeOptions()
option.add_argument("--incognito")
browser = webdriver.Chrome(executable_path='C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.5\chrome webdriver\chromedriver', chrome_options=option)
browser.get("https://www.tripadvisor.in/Attraction_Review-g297683-d317329-Reviews-Taj_Mahal-Agra_Agra_District_Uttar_Pradesh.html")
time.sleep(10)
browser.find_element_by_xpath('//*[@id="taplc_location_review_filter_controls_0_form"]/div[4]/ul/li[5]/a').click()
time.sleep(5)
browser.find_element_by_xpath('//*[@id="BODY_BLOCK_JQUERY_REFLOW"]/span/div[1]/div/form/ul/li[2]/label').click()
time.sleep(5)
while (pages):
html = browser.page_source
soup = BeautifulSoup(html, "html.parser")
containers = soup.find_all("div",{"class":"innerBubble"})
showMore = soup.find("span", {"onclick": "widgetEvCall('handlers.clickExpand',event,this);"})
if showMore:
browser.find_element_by_xpath("//span[@onclick=\"widgetEvCall('handlers.clickExpand',event,this);\"]").click()
time.sleep(3)
html = …Run Code Online (Sandbox Code Playgroud) 我正在使用 python 检索 HTML 源代码,但结果如下所示。这是什么?为什么我没有获得实际的页面源代码?
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C
python ×4
utf-8 ×2
encoding ×1
html ×1
mojibake ×1
python-3.x ×1
selenium ×1
unicode ×1
web-scraping ×1