Noob: Joined headers on webscrape, need to split into parent/child - web-scraping

I'm simply compiling a list of phone brands, models, and prices etc from a telco website... and through trial and error I'm starting out ok (from a noob position) but the Headers are appearing as a joint string as it appears the html is designed to be individual and not parent/child or attributes per say?
Help/guidance would be appreciated...
Script:
import pandas
import requests
from bs4 import BeautifulSoup
url = 'https://www.vodafone.com.au/mobile-phones'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
headers = soup.find_all('h2')
divs = soup.find_all('div')
model = list(map(lambda h: h.text.strip(), headers))
print(model)
Result:
[
"AppleiPhone 14 Pro Max",
"AppleiPhone 14 Pro",
"AppleiPhone 14 Plus",
"AppleiPhone 14",
"SamsungSamsung Galaxy Z Fold4 5G",
"SamsungSamsung Galaxy Z Flip4 5G",
"SamsungSamsung Galaxy S22 Ultra 5G",
"AppleiPhone 13",
"GoogleGoogle Pixel 7 Pro",
"GoogleGoogle Pixel 7",
"AppleiPhone 12",
"AppleiPhone 11",
"SamsungSamsung Galaxy S22 5G",
"SamsungSamsung Galaxy A13 5G",
"SamsungSamsung Galaxy A13 4G",
"GoogleGoogle Pixel 6 Pro",
"GoogleGoogle Pixel 6a",
"OPPOOPPO Find X5 Pro 5G",
"OPPOOPPO A57 4G",
"OPPOOPPO Reno8 5G",
"SamsungSamsung Galaxy A53 5G",
"SamsungSamsung Galaxy A33 5G",
"SamsungSamsung Galaxy Z Fold3 5G",
"SamsungSamsung Galaxy S21+ 5G",
"SamsungSamsung Galaxy S21 Ultra 5G",
"SamsungSamsung Galaxy S21 FE 5G",
"AppleiPhone SE (3rd gen)",
"TCLTCL 20 Pro 5G",
"MotorolaMotorola moto g62 5G",
"SamsungSamsung Galaxy A73 5G",
"OPPOOPPO Find X5 5G",
"OPPOOPPO Find X5 Lite 5G",
"MotorolaMotorola moto e22i 4G",
"MotorolaMotorola edge 30 pro 5G",
"MotorolaMotorola edge 30 5G",
"Why choose Vodafone?"
]
~Ideal result:
Apple; iPhone 14 Pro Max, Apple; iPhone 14 Pro, Apple; iPhone 14 Plus,
Apple; iPhone 14,

You can find the manufacturer and device by selecting from the headers.
import requests
from bs4 import BeautifulSoup
url = 'https://www.vodafone.com.au/mobile-phones'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
headers = soup.find_all('h2')
brand_device = []
for header in headers:
manufacturer_div = header.select('div[class*="__Manufacturer-"]')
device_div = header.select('div[class*="__Name-"]')
if (len(manufacturer_div) > 0 and len(device_div) > 0):
brand_device.append(
f'{manufacturer_div[0].text.strip()};{device_div[0].text.strip()}')
print(brand_device)
Feel free to format the result for your need.

Related

src_indices best practices

After ver. 3.13 src_indices is changed, I could not find it in docs. Could you share a sample code for connecting a smaller portion of a large matrix?
i.e. source is 5x5 matrix and target is 2x2 starting from [2,3]
I find it easiest to think about connections as slices. We added om.slicer to handle indices via slicing notation. The following example connects a 2x2 portion of a 5x5 matrix, starting from indices [2, 3]:
import openmdao.api as om
import numpy as np
print('Connecting a 2x2 submatrix of M to B')
p = om.Problem()
ivc = p.model.add_subsystem('ivc', om.IndepVarComp())
ivc.add_output('M', val=np.arange(25).reshape((5, 5)))
exec = p.model.add_subsystem('exec', om.ExecComp())
exec.add_expr('A = B', A={'shape': (2, 2)}, B={'shape': (2, 2)})
p.model.connect('ivc.M', 'exec.B', src_indices=om.slicer[2:4, 3:5])
p.setup()
p.run_model()
print('M')
print(p.get_val('ivc.M'))
print('A')
print(p.get_val('exec.A'))
Connecting a 2x2 submatrix of M to B
M
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]
[20. 21. 22. 23. 24.]]
A
[[13. 14.]
[18. 19.]]
Or, for instance, connecting only rows [0, 2, 3] of the 5x5 matrix to the target:
rint('Connecting rows 0, 2, and 3 of M to B')
p = om.Problem()
ivc = p.model.add_subsystem('ivc', om.IndepVarComp())
ivc.add_output('M', val=np.arange(25).reshape((5, 5)))
exec = p.model.add_subsystem('exec', om.ExecComp())
exec.add_expr('A = B', A={'shape': (3, 5)}, B={'shape': (3, 5)})
p.model.connect('ivc.M', 'exec.B', src_indices=om.slicer[[0, 2 ,3], :])
p.setup()
p.run_model()
print('M')
print(p.get_val('ivc.M'))
print('A')
print(p.get_val('exec.A'))
Connecting rows 0, 2, and 3 of M to B
M
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]
[20. 21. 22. 23. 24.]]
A
[[ 0. 1. 2. 3. 4.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]]
While we discuss this feature in some of the parallelization docs, you're correct that we need to discuss it in our connection documentation.

Can I use Instantaneous screw with Finite screw?

I'm trying to calculate angular velocity of each axis by movement of end-effector,
If S1 and S2 is finite screw, and S2 had infinitesimal movement from S1.
Also, let S1_ be (-)array of S1 and instantaneous screw of S1 is St1
So If I triangle product S2 and S1_ (S2△S1_=St1), It becomes almost instantaneous screw of S1 (I believe)
What I want to calculate is, if St1 is instantaneous screw, than can I calculate the angular velocity of each axis by using inverse jacobian with [ (J^-1)*St1 = answer ]?
(jacobian is from S1, if S1=S1_6△S1_5△S1_4△S1_3△S1_2△S1_1, (the robot has 6 axis),
jacobian matrix 'J' = [Su1_1, Su1_2, Su1_3, Su1_4, Su1_5, Su1_6], Su is for 'unit twist')
When you look at the kinematics recursively from the base to the end effector you have
vi = vi-1 + si ui
where vi is the velocity screw of each link, vi-1 is the velocity screw of the previous link, si is the unit screw of the joint axis, and ui the joint speed.
So the end effector has a final velocity screw of
v6 = s1 u1 + s2 u2 + s3 u3 + s4 u4 + s5 u5 + s6 u6
and I think you are asking on how to find the vector of joint speeds u = (u1, u2, u3, u4, u5, u6)
So you compose the 6×6 jacobian matrix J, by combining the individual joint axis unit screws in columns
J = [ s1 s2 s3 s4 s5 s6]
and invert the kinematics
v6 = J * u ⇒ u = J-1 v6

calculating pixel size from ppi

I need some help to verify that i have made my calculations correct.
I want to determine number of pixels in the span of 13.6 millimeters for a specific device that is 224,17 pixels per inch.
( if 1 inch = 224.17ppi therefor: 1 centimeter = 569.39ppcm therefor: 1 millimeter = 5.69 ppmm )
I calculate 13.6mm = 77.35 pixels
(224.17 pixels / 1 inch) * (1 inch / 25.4 mm) * 13.6 mm = 120 pixels
you can user converter for your ease like
http://endmemo.com/sconvert/millimeterpixel.php

How can I solve this precedence graph?

This is my class assignment and I don't know how I can construct a precedence graph for :
S1: x=0
S2: x=x+1
S3: y=2
S4: z=y
S5: x=x+2
S6: y=x+z
S7: z=4
I thought it would be like:
S1 goes to S2, S5, and S6
S2 goes to S5 and S6
S3 goes to S4 and S6
S4 goes to S6
S5 goes to S6
S7 goes to S6
but S7 doesn't go to S6 in the answer.
It looks S7 goes to S6 because the right hand side of S6 has "z".
Please help me out this problem.
The example of precedence graph
s7 can not goooo to s6 because once a program is executed it can be reversed.
Moreover s1 can go to s3 s4 or any number below it but s2 can not refer back to s1.
I hope you understand that :)

How to print output of .bas file to text

I am trying to print coordinate outputs of a program to a text file in order to use it in another program but I don't really know anything about GWBASIC and its my first time using MS-DOS. I need it to open a text file named plot.txt and print output there and save it without actually plotting on GWBASIC. Here is the program which I found in an old magazine.
810 REM MAKE A GLOBULAR
12 REM
14 R0=20: R2=R0*R0: R3=R2*R0
16 P1=3.14159265#
18 C0=P1*P1*R3/4
20 R1=R0/SQR(2)
22 XM=512: YM=512
24 X2=XM/2: Y2=YM/2: S=5
26 INPUT "HOW MANY STARS ";T
27 RANDOMIZE TIMER
28 CLS: REM CLEAR SCREEN
30 FOR I=1 TO T
32 C=C0*RND: R=R1
34 REM
36 REM NOW FIND R
38 FOR K=1 TO 5
40 GOSUB 100
42 R=R+(C-C1)/D
44 NEXT K
46 REM 3-DIMENSIONAL PLACE
48 X=RND-.5
50 Y=RND-.5
52 Z=RND-.5
54 S1=SQR(X*X+Y*Y+Z*Z)
56 IF S1>.5 THEN GOTO 48
58 REM POINT IS NOW IN SPHERE
60 R=R*S1: X=X*R: Y=Y*R: Z=Z*R
62 GOSUB 200
64 NEXT I
66 END
68 REM
100 REM NEWTON-RAPHSON ITERATION
105 A=R/R0
110 C1=ATN(A)*.5*R3
115 A=1+A*A
120 C1=C1+R*.5*R2/A
125 C1=P1*(C1-R*R2/(A*A))
130 D=4*P1*R*R/(A*A*A)
135 RETURN
140 REM
200 REM 2-DIMENSIONAL PLOT
203 SCREEN 9
205 X=X*S+X2: Y=Y*S+Y2
210 IF X<0 OR Y<0 THEN 225
215 IF X>=XM OR Y>=YM THEN 225
220 PSET(X,Y)
225 RETURN
230 REM ------------------------
240 REM APPEARED IN ASTRONOMICAL
250 REM COMPUTING, SKY & TELE-
260 REM SCOPE, APRIL, 1986
270 REM ------------------------
Here is a Python 3 paraphrase:
#globular.py
#Python paraphrase of model.bas from
#http://www.skyandtelescope.com/wp-content/uploads/model.bas
from math import pi, sqrt, atan
from random import uniform, random
#Global variables:
r0 = 20.0
r2 = r0**2
r3 = r0**3
c0 = pi**2*r3/4
r1 = r0/sqrt(2)
def NRI(c,r):
#Newton-Raphson Iteration
a = r/r0
c1 = atan(a)*0.5*r3
a = 1+a**2
c1 += r*0.5*r2/a
c1 = pi*(c1-r*r2/a**2)
d = 4*pi*r**2/a**3
return (c1,d)
def makeStars(t):
stars = []
for i in range(t):
c = c0*random()
r = r1
for k in range(5):
c1,d = NRI(c,r)
r += (c-c1)/d
while True:
x = uniform(-0.5,0.5)
y = uniform(-0.5,0.5)
z = uniform(-0.5,0.5)
s1 = sqrt(x**2 + y**2 + z**2)
if s1 <= 0.5: break
r *= s1
x *= r
y *= r
z *= r
stars.append((x,y,z))
return stars
def starsToFile(t,fname):
stars = makeStars(t)
f = open(fname,'w')
for star in stars:
print(*star, sep = ', ',file = f)
f.close()
I skipped the part about printing x and y and instead wrote a function makeStars to return a list of (x,y,z) tuples, as well as a related function which takes such an output and sends it to a text file. This last function is the only thing that used Python 3 instead of Python 2. If you are using Python 2 you can import Python 3's print function from the future.
Typing starsToFile(100,'stars.txt') in the Python shell gave me a text file which begins:
-0.32838465248713156, -0.3294895266926551, -1.2963580524762535
14.20224408569865, 1.4434961933043464, 6.450969593697097
1.6525937589658193, -0.24447292610082685, 1.0543647986350608
1.5707528567123823, 5.190972598268825, -2.0054790217091134
I don't have good 3-d scatter-plot graphing at my finger tips, but here is a screen shot of 50 points generated by the function and plotted using a computer algebra system called Derive:
Final remark: I wonder if there is a typo in the source code. The line
C0=P1*P1*R3/4
strikes me as suspicious since it is fairly rare in mathematics for pi to appear squared -- though it does happen. Maybe there should be only 1 factor of pi there (which would then have the effect of setting C0 proportional to the volume of the sphere of radius R0). On the other hand, I don't know exactly what is happening here, so I left it in. If the results seem problematic, you could maybe experiment with that line.
If you want a copy of the calculated coordinates simply add these lines:
1 OPEN "PLOT.TXT" FOR OUTPUT AS #1
65 CLOSE #1
221 PRINT #1, X + "," + Y
The program will work as before but in addition to this it outputs the coordinate to a file named plot.txt
Put them in an image with 640x350 size (that size is demanded by SCREEN 9) and you get the same result.

Resources