我在 python 3.2 中使用 bs4 (beautifulsoup),这是我的代码:
from urllib import urlopen
from bs4 import bs4
import re
webpage = urlopen(‘http://www.azlyrics.com/lyrics/kanyewest/workoutplan.html’).read()
Run Code Online (Sandbox Code Playgroud)
它给:
webpage = urlopen(‘http://www.azlyrics.com/lyrics/kanyewest/workoutplan.html’).read()
^
SyntaxError: invalid character in identifier
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
感谢 PhantomJS,我正在尝试抓取此网页:https : //www.koshkamashkaeshop.com/fr/28-robes-Koshka-Mashka 但每次都无法加载。我认为这是因为https。这是我的 .sh 代码:
phantomjs --ignore-ssl-errors=yes test.js
Run Code Online (Sandbox Code Playgroud)
这是我的 test.js 代码:
page.open(url, function (status) {
var content = page.evaluate(function()
{
if (status !== 'success') {
console.log('FAIL to load the address');
}else{
}
}
)})
Run Code Online (Sandbox Code Playgroud) 我正在使用 cURL 来获取页面源。效果很好,但是当我使用
echo '<pre>';
print_r($source);
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)
仅显示为文本,页面被解析,我最终将其视为网页而不是纯文本。
如何仅以文本形式查看远程页面?
我是一个硒菜鸟,一直在努力用 python 完成工作。我试图从这个页面迭代所有用户评论(“partial_entry”类)https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-or560-TAP-Portugal#REVIEWS
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome("C:\Users\shalini\Downloads\chromedriver_win32\chromedriver.exe")
driver.maximize_window()
url="https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-or560-TAP-Portugal#REVIEWS"
driver.get(url)
for i in driver.find_elements_by_xpath("//div[@class='wrap']"):
print i.find_element(By.XPATH, '//p[@class="partial_entry"]')
print i.text
print "=============================================="
# THIS IF BLOCK IS NECESSARY, I CANT DO AWAY WITH THIS ONE
if i.find_elements(By.CSS_SELECTOR,"#REVIEWS .googleTranslation>.link"):
print "======YES TRANSLATION AVAILABLE========"
Run Code Online (Sandbox Code Playgroud)
即使我每次在 for 循环中选择一个不同的元素,但它一遍又一遍地打印相同的元素。(我必须保留最后一个 if 块并且不能取消它,所以无论解决方案是什么,它也必须包含 if 块)
======编辑==================
即使这也不起作用(根据http://selenium-python.readthedocs.io/locating-elements.html,这实际上应该起作用)。我不知道硒是怎么回事!!!!!
print i.find_element(By.CSS_SELECTOR, 'p.partial_entry')
Run Code Online (Sandbox Code Playgroud)
输出:
NoSuchElementException:
Run Code Online (Sandbox Code Playgroud) from requests import get
get('http://www.fb.com')
<Response [200]>
get('http://www.subscene.com')
<Response [403]
Run Code Online (Sandbox Code Playgroud)
我正在尝试构建一个网络抓取工具来抓取和下载字幕。但是我无法请求任何字幕页面,因为它们返回响应代码 403。
我从https://automatetheboringstuff.com学会了如何废弃网站。我想报废http://www.piaotian.net/html/3/3028/1473227.html,里面的内容是中文的,写成.txt文件。但是,.txt 文件包含随机符号,我认为这是编码/解码问题。
我读过这个线程“如何使用 python 解码和编码网页? ”并认为我的网站的编码方法是“gb2312”和“windows-1252”。我尝试在这两种编码方法中解码但失败了。
有人可以向我解释我的代码的问题吗?我对编程很陌生,所以也请让我知道我的误解!
此外,当我从代码中删除“html.parser”时,.txt 文件原来是空的,而不是至少有符号。为什么会这样?
import bs4, requests, sys
reload(sys)
sys.setdefaultencoding("utf-8")
novel = requests.get("http://www.piaotian.net/html/3/3028/1473227.html")
novel.raise_for_status()
novelSoup = bs4.BeautifulSoup(novel.text, "html.parser")
content = novelSoup.select("br")
novelFile = open("novel.txt", "w")
for i in range(len(content)):
novelFile.write(str(content[i].getText()))
Run Code Online (Sandbox Code Playgroud) 我试图从basketball-reference.com 抓取球队统计数据网页,但是当我使用readHTML 时,它只会带回前两个表格。
我的 R 代码如下所示:
url = "http://www.basketball-reference.com/leagues/NBA_2015.html"
teamPageTables = readHTMLTable(url)
Run Code Online (Sandbox Code Playgroud)
这将返回一个只有 2 个的列表。页面上的前两个表。我希望有一个包含页面中所有表格的列表。
我还尝试将 rvest 与我想要的表(杂项统计表)的 XPath 一起使用,但也没有运气。
BBR 是否更改了一些内容以阻止抓取。我什至看到了其他关于抓取团队网站的帖子,该网站指出他想要的表位于索引 16 ......我复制了他的代码,但仍然没有。
任何帮助将不胜感激。谢谢,
我正在使用 node.js/cheerio(与 jQuery 语法相同)来解析页面。当我使用时:
$ = cheerio.load(body);
console.log($('.td-title a').text());
Run Code Online (Sandbox Code Playgroud)
我的控制台中有很长的字符串,来自单词“mainaboutcontactusprofile”等。如何制作一组 td 文本?
我正在尝试使用 Scrapy 将篮球队的时间表保存到 CSV 文件中。我在这些文件中编写了以下代码:
设置.py
BOT_NAME = 'test_project'
SPIDER_MODULES = ['test_project.spiders']
NEWSPIDER_MODULE = 'test_project.spiders'
FEED_FORMAT = "csv"
FEED_URI = "cportboys.csv"
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'test_project (+http://www.yourdomain.com)'
# Obey robots.txt rules
ROBOTSTXT_OBEY = True
Run Code Online (Sandbox Code Playgroud)
khsabot.py
import scrapy
class KhsaabotSpider(scrapy.Spider):
name = 'khsaabot'
allowed_domains = ['https://scoreboard.12dt.com/scoreboard/khsaa/kybbk17/?id=51978']
start_urls = ['http://https://scoreboard.12dt.com/scoreboard/khsaa/kybbk17/?id=51978/']
def parse(self, response):
date = response.css('.mdate::text').extract()
opponent = response.css('.opponent::text').extract()
place = response.css('.schedule-loc::text').extract()
for item in zip(date,opponent,place):
scraped_info = {
'date' : item[0], …Run Code Online (Sandbox Code Playgroud) 我正在通过 Python 使用 Selenium 来尝试网络抓取。我几乎到了我想去的地方,但我遇到了我现在意识到的问题。所以我正在使用的元素是这样的:
<td class=" ui-datepicker-days-cell-over ui-datepicker-current-day ui-datepicker-today"
data-handler="selectDay" data-event="click" data-month="3" data-year="2018">
<a class="ui-state-default ui-state-highlight ui-state-active" href="#">10
</a>
</td>
Run Code Online (Sandbox Code Playgroud)
我的最终目标是获得 a 标签之间的 10 个。到目前为止,这是我的代码:
option = selenium.webdriver.ChromeOptions()
option.add_argument(" - incognito")
browser = webdriver.Chrome(executable_path=r"chromedriver.exe")
browser.get(myUrl)
calendar = browser.find_element_by_xpath(
'/html/body/main/section/div[2]/div[1]/div[2]/div[3]/div/div[1]/div/div[1]/div[2]')
viewCal = browser.find_element_by_name('choice_set[begin_at]')
viewCal.click()
row = calendar.find_elements_by_tag_name('tr')
column = calendar.find_elements_by_tag_name('td')
numb = column[0].find_element_by_tag_name('a')
numb.text
Run Code Online (Sandbox Code Playgroud)
numb.text返回''而不是 10。
我在这里做错了什么?
python selenium web-scraping python-3.x selenium-chromedriver
web-scraping ×10
python ×4
javascript ×2
python-3.x ×2
selenium ×2
cheerio ×1
curl ×1
decoding ×1
encoding ×1
jquery ×1
node.js ×1
phantomjs ×1
php ×1
python-2.7 ×1
r ×1
scrapy ×1
ssl ×1
terminal ×1