Using scrapy in raspberry pi with python

Problem:

You need to extract some information from a public website an process it to do "thinks"

Solution:

One solution is to use a web scrap tool, to let you know who it works, lets see a simple example to extract metada from  single url in a single program.

1.- Install scrappy and resolve any dependence issue, maybe you need to upload crypto library [sudo pip3 install cryptography==2.8]

sudo pip3 install scrapy

2.- Check scrappy release

scrapy version

3.- Use the next code in file "myscrapbot.py"

#get links from a single page without jump
import scrapy
class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'https://raspberrypihack.blogspot.com/',
    ]
    def parse(self, response):
            yield {
                'enlaces': response.xpath('//a/@href').extract(),
            }

4.- Run the program using "scrapy"

scrapy runspider myscrapbot.py -o export.json
4.- The data selected to be scraped will be saved in file export.json

5.- How to extract data from a data base table urls

First, you have to checl the disscussion in this post.

Then you have to addapt the code using this post recomendation from the first answers.

import scrapy

import mysql.connector

class ProductsSpider(scrapy.Spider):
    name = "Products"
    start_urls = []
    def parse(self, response):
 
      print(self.start_urls)
         yield {
              'enlaces': response.xpath('//a/@href').extract(),
        }
    def start_requests(self):
        conn = mysql.connector.connect(
                user='user',
                passwd='_a30e4qK',
                db='DDBB',
                host='localhost',
                charset="utf8",

                use_unicode=True
                )
        cursor = conn.cursor()
        cursor.execute('SELECT links FROM CUSTOMERS;')
        rows = cursor.fetchall()
        for row in rows:
           ProductsSpider.start_urls.append(row[0])
           yield self.make_requests_from_url(row[0])
        conn.close()




X.- How to extract href data?

response.xpath('//a/@href').extract()
response.xpath('//a/text()').extract()
more will continue soon...


Source:

Connect to MySQL using Python

Problem:

You need to connect to a external MySQL database and do thing...

Solution:

1.- Install mysql drivers

sudo python3 -m pip install mysql-connector-python

2.- Create a new program using the next code:
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)

Sources:

3.- Do thinks... 

https://www.w3schools.com/python/python_mysql_getstarted.asp

Raspberry Pi 4 release 1.1 or 1.2 ? what is the diference ?

Problem:

You need to know the correct release of your Raspberry Pi but it's not possible to identify it in the board, neither the storage box neither the product technical documentation.

Solution:

Run the next command:

cat /proc/cpuinfo

Check the information in the source link to identify the release of your model using the map table in the official Raspberry Pi website

Source:

https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md

 

How to connect a OLED display with a Raspberry Pi

Problem:

You would like to use a OLED display with a I2C interface with a Raspberry pi using a SH1106 controller , 

Solution:

That type of OLE Display have 4 connections, just follow the next wire schema to connect it to the Raspberry.

Connect the wires directly between the oled display and the Raspberry:

OLED pins >> Raspberry Pi

VDD >> 5V

GND >> GND

SCK >> SCL  ( more information )

SDA >> SDA  ( more information)

*Check the panel in your OLED display to ensure the correct pin definition in the oled panel 

Setup the software:

Activate the I2C Interface option in system configuration, use the command raspi-config and go to interface settings, there you will fine the I2C option.

Install libraries:

Just use the following command: 

sudo apt-get install -y python-smbus 
sudo apt-get install -y i2c-tools
sudo apt-get reinstall build-essential
sudo apt-get install i2c-tools
$ sudo apt-get update
$ sudo apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 -y
$ sudo -H pip3 install luma.oled

Check if the system detect it:

You can check it if you find signal in this interface map, use the
command: sudo i2cdetect -y 1

If it works, you will see something like this:



Working with the Oled and Python:
1.- Clonning the exmaple repository:
$ git clone https://github.com/rm-hull/luma.examples.git
$ cd luma.examples

2.- install libraries
sudo -H pip install -e .

3.- run examples
cd examples
sudo python3 3d_box.py

Hardware: