Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web Scraper(Python 3.6)在遇到字符串中的javascript时崩溃_Javascript_Python 3.x_Web Scraping_Crash - Fatal编程技术网

Web Scraper(Python 3.6)在遇到字符串中的javascript时崩溃

Web Scraper(Python 3.6)在遇到字符串中的javascript时崩溃,javascript,python-3.x,web-scraping,crash,Javascript,Python 3.x,Web Scraping,Crash,为此,我用BeautifulSoup编写了一个(可能效率低下的)web刮板。当它工作时,获取帖子的函数在遇到一个包含javascript的帖子时崩溃,因为循环通过帖子内容(for item in i.find_all(“p”)[1::)停止,并且稍后对元数据的请求(i.select('span')[0])。帖子的get_text())无法找到特定元素。最后一篇文章就是一个例子。虽然我可以编写异常代码,但我更愿意理解问题并直接解决它。我做错了什么 from urllib.request impor

为此,我用BeautifulSoup编写了一个(可能效率低下的)web刮板。当它工作时,获取帖子的函数在遇到一个包含javascript的帖子时崩溃,因为循环通过帖子内容(
for item in i.find_all(“p”)[1::
)停止,并且稍后对元数据的请求(
i.select('span')[0])。帖子的get_text()
)无法找到特定元素。最后一篇文章就是一个例子。虽然我可以编写异常代码,但我更愿意理解问题并直接解决它。我做错了什么

from urllib.request import urlopen
import requests as rs
from bs4 import BeautifulSoup as BS
import re
from itertools import chain
posts = []
def post_data(postlist, weblink, rmin, rmax):
    page = rs.get(weblink)
    soup = BS(page.content, 'lxml')
    for d in range(rmin, rmax):
        for i in soup.find_all("div", id="position-"+str(d)):
            text = []
            for item in i.find_all("p")[1:]:
                text.append(item.get_text().replace("\n" , "/" ).replace("," , "$" ))
            text = "".join(text)
            text.replace("\n", "/").replace("," , "$" )
            postlist.append((weblink, str(d), i.find("strong").get_text() , text , i.select('span')[0].get_text(), i.select('span')[1].get_text(), i.span["id"][1:], list(i.find("div", class_="poststuff"))[0]))
    postlist=list((chain.from_iterable(postlist)))
post_data(posts, "http://www.poliscirumors.com/topic/tenure-denial-blog/page/23", 460, 461)
错误如下:

File "p3.py", line 20, in post_data
postlist.append((weblink, str(d), i.find("strong").get_text() , text , i.select('span')[0].get_text(), i.select('span')[1].get_text(), i.span["id"][1:], list(i.find("div", class_="poststuff"))[0]))
IndexError: list index out of range
始终尝试,然后稍后调试 尝试: postlist.append((weblink,str(d),i.find(“strong”).get_text(),text,i.select('span')[0]。get_text(),i.select('span')[1]。get_text(),i.span[“id”][1:],list(i.find(“div”,class=“poststuff”)[0]))

除: 通过

始终尝试,然后稍后调试 尝试: postlist.append((weblink,str(d),i.find(“strong”).get_text(),text,i.select('span')[0]。get_text(),i.select('span')[1]。get_text(),i.span[“id”][1:],list(i.find(“div”,class=“poststuff”)[0]))

除:
通过

哦,谢谢。吸取了教训。当然,这是可行的。尽管你知道它为什么会崩溃吗?javascript中呈现的任何东西都给了我凝灰岩的时间。我必须使用我正在使用的链接检索到json响应。希望有更聪明的人看到这篇文章。谢谢。吸取了教训。当然,这是可行的。尽管你知道它为什么会崩溃吗?javascript中呈现的任何东西都给了我凝灰岩的时间。我必须使用我正在使用的链接检索到json响应。希望有更聪明的人看到这篇文章。干杯