CrawlerProcess vs CrawlerRunner

Scrapy’s documentation does a pretty bad job at giving examples on real applications of both. CrawlerProcess assumes that scrapy is the only thing that is going to use twisted’s reactor. If you are using threads in python to run other code this isn’t always true. Let’s take this as an example. from scrapy.crawler import CrawlerProcess … Read more

How to convert raw javascript object to a dictionary?

demjson.decode() import demjson # from js_obj = ‘{x:1, y:2, z:3}’ # to py_obj = demjson.decode(js_obj) chompjs.parse_js_object() import chompjs # from js_obj = ‘{x:1, y:2, z:3}’ # to py_obj = chompjs.parse_js_object(js_obj) jsonnet.evaluate_snippet() import json, _jsonnet # from js_obj = ‘{x:1, y:2, z:3}’ # to py_obj = json.loads(_jsonnet.evaluate_snippet(‘snippet’, js_obj)) ast.literal_eval() import ast # from js_obj = “{‘x’:1, … Read more

How to handle IncompleteRead: in python

The link you included in your question is simply a wrapper that executes urllib’s read() function, which catches any incomplete read exceptions for you. If you don’t want to implement this entire patch, you could always just throw in a try/catch loop where you read your links. For example: try: page = urllib2.urlopen(urls).read() except httplib.IncompleteRead, … Read more

“Failed to decode response from marionette” message in Python/Firefox headless scraping script

For anyone else experiencing this issue when running selenium webdriver in a Docker container, increasing the container size to 2gb fixes this issue. I guess this affects physical machines too if the OP fixed their issue by upgrading their server RAM to 2Gb, but could be coincidence.

BeautifulSoup: Get the contents of a specific table

This is not the specific code you need, just a demo of how to work with BeautifulSoup. It finds the table who’s id is “Table1” and gets all of its tr elements. html = urllib2.urlopen(url).read() bs = BeautifulSoup(html) table = bs.find(lambda tag: tag.name==’table’ and tag.has_attr(‘id’) and tag[‘id’]==”Table1″) rows = table.findAll(lambda tag: tag.name==’tr’)

BeautifulSoup webscraping find_all( ): finding exact match

In BeautifulSoup 4, the class attribute (and several other attributes, such as accesskey and the headers attribute on table cell elements) is treated as a set; you match against individual elements listed in the attribute. This follows the HTML standard. As such, you cannot limit the search to just one class. You’ll have to use … Read more